87 lines
2.9 KiB
C#
87 lines
2.9 KiB
C#
using System.IO;
|
|
using System.Text;
|
|
|
|
namespace CommonLibrary
|
|
{
|
|
public static class Extensions
|
|
{
|
|
//TODO: 제거후 PathCombine 에 통합
|
|
public static string PathCombineL(params string[] paths)
|
|
{
|
|
var directorySeparatorChar = System.IO.Path.AltDirectorySeparatorChar.ToString();
|
|
return PathCombine(directorySeparatorChar, paths).Replace("\\", "/");
|
|
}
|
|
|
|
//TODO: 제거예정
|
|
public static string PathCombineW(params string[] paths)
|
|
{
|
|
var directorySeparatorChar = System.IO.Path.DirectorySeparatorChar.ToString();
|
|
return PathCombine(directorySeparatorChar, paths).Replace("/", "\\");
|
|
}
|
|
|
|
static string PathCombine(string directorySeparator, params string[] paths)
|
|
{
|
|
if (paths.Length == 0) return string.Empty;
|
|
|
|
var path = new StringBuilder();
|
|
path.Append(paths[0]);
|
|
|
|
for (int i = 1; i < paths.Length; i++)
|
|
{
|
|
if (path.ToString().Substring(path.Length - 1, 1) == directorySeparator)
|
|
{
|
|
if (paths[i].Substring(0, 1) == directorySeparator)
|
|
{
|
|
path.Append(paths[i].Substring(1, paths[i].Length - 1));
|
|
}
|
|
else
|
|
{
|
|
path.Append(paths[i]);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (paths[i].Substring(0, 1) == directorySeparator)
|
|
{
|
|
path.Append(paths[i]);
|
|
}
|
|
else
|
|
{
|
|
path.Append(directorySeparator);
|
|
path.Append(paths[i]);
|
|
}
|
|
}
|
|
}
|
|
|
|
return path.ToString();
|
|
}
|
|
|
|
public static bool IsDefault<T>(ref this T data) where T : struct
|
|
=> default(T).Equals(data);
|
|
|
|
public static void DeepMove(this DirectoryInfo sourceDirectoryInfo, DirectoryInfo targetDirectoryInfo)
|
|
{
|
|
if (!sourceDirectoryInfo.Exists) return;
|
|
|
|
if (sourceDirectoryInfo.Root.Name == targetDirectoryInfo.Root.Name)
|
|
{
|
|
sourceDirectoryInfo.MoveTo(targetDirectoryInfo.FullName);
|
|
}
|
|
else
|
|
{
|
|
var fileInfos = sourceDirectoryInfo.GetFiles("*", SearchOption.AllDirectories);
|
|
|
|
foreach (var file in fileInfos)
|
|
{
|
|
var targetDirectory = new DirectoryInfo(file.DirectoryName.Replace(sourceDirectoryInfo.FullName, targetDirectoryInfo.FullName));
|
|
targetDirectory.Create();
|
|
var targetFileName = System.IO.Path.Combine(targetDirectory.FullName, file.Name);
|
|
|
|
file.CopyTo(targetFileName, true);
|
|
}
|
|
|
|
sourceDirectoryInfo.Delete(true);
|
|
}
|
|
}
|
|
}
|
|
} |