97 lines
3.4 KiB
C#
97 lines
3.4 KiB
C#
using System.IO;
|
|
using System.Text;
|
|
|
|
namespace CommonLibrary
|
|
{
|
|
public static class Extensions
|
|
{
|
|
public static string PathCombineL(params string[] paths)
|
|
{
|
|
string directorySeparatorChar = System.IO.Path.AltDirectorySeparatorChar.ToString();
|
|
return PathCombine(directorySeparatorChar, paths).Replace("\\", "/");
|
|
}
|
|
|
|
public static string PathCombineW(params string[] paths)
|
|
{
|
|
string directorySeparatorChar = System.IO.Path.DirectorySeparatorChar.ToString();
|
|
return PathCombine(directorySeparatorChar, paths).Replace("/", "\\");
|
|
}
|
|
|
|
private static string PathCombine(string directorySeparator, params string[] paths)
|
|
{
|
|
if (paths.Length == 0)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
StringBuilder 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
|
|
{
|
|
return default(T).Equals(data);
|
|
}
|
|
|
|
public static void DeepMove(this DirectoryInfo sourceDirectoryInfo, DirectoryInfo targetDirectoryInfo)
|
|
{
|
|
if (!sourceDirectoryInfo.Exists)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (sourceDirectoryInfo.Root == targetDirectoryInfo.Root)
|
|
{
|
|
sourceDirectoryInfo.MoveTo(targetDirectoryInfo.FullName);
|
|
return;
|
|
}
|
|
|
|
DirectoryInfo[] directoryInfos = sourceDirectoryInfo.GetDirectories("*", SearchOption.AllDirectories);
|
|
FileInfo[] fileInfos = sourceDirectoryInfo.GetFiles("*", SearchOption.AllDirectories);
|
|
|
|
foreach (var directoryInfo in directoryInfos)
|
|
{
|
|
DirectoryInfo targetDirectory = new DirectoryInfo(directoryInfo.FullName.Replace(sourceDirectoryInfo.FullName, targetDirectoryInfo.FullName));
|
|
if (!targetDirectory.Exists)
|
|
{
|
|
targetDirectory.Create();
|
|
}
|
|
}
|
|
|
|
foreach (var file in fileInfos)
|
|
{
|
|
DirectoryInfo targetDirectory = new DirectoryInfo(file.DirectoryName.Replace(sourceDirectoryInfo.FullName, targetDirectoryInfo.FullName));
|
|
string targetFileName = System.IO.Path.Combine(targetDirectory.FullName, file.Name);
|
|
|
|
file.CopyTo(targetFileName, true);
|
|
}
|
|
sourceDirectoryInfo.Delete(true);
|
|
}
|
|
}
|
|
} |