|
|
|
|
|
// ***********************************************************************
|
|
|
|
|
|
// Assembly : Construction
|
|
|
|
|
|
// Author : zjx
|
|
|
|
|
|
// Created : 11-13-2020
|
|
|
|
|
|
//
|
|
|
|
|
|
// Last Modified By : zjx
|
|
|
|
|
|
// Last Modified On : 11-13-2020
|
|
|
|
|
|
// ***********************************************************************
|
|
|
|
|
|
// <copyright file="IO.cs" company="jindongfang">
|
|
|
|
|
|
// Copyright (c) jindongfang. All rights reserved.
|
|
|
|
|
|
// </copyright>
|
|
|
|
|
|
// <summary></summary>
|
|
|
|
|
|
// ***********************************************************************
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Fk.Utils
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Class IO.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static class WorkAreaDirectoryIO
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Copies the file.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sourceFileName">Name of the source file.</param>
|
|
|
|
|
|
/// <param name="destFileName">Name of the dest file.</param>
|
|
|
|
|
|
/// <param name="overwrite">if set to <c>true</c> [overwrite].</param>
|
|
|
|
|
|
public static void CopyFile(string sourceFileName, string destFileName, bool overwrite)
|
|
|
|
|
|
{
|
|
|
|
|
|
string dir = Path.GetDirectoryName(destFileName);
|
|
|
|
|
|
|
|
|
|
|
|
if (!Directory.Exists(dir))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(dir);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (overwrite)
|
|
|
|
|
|
{
|
|
|
|
|
|
System.IO.File.Copy(sourceFileName, destFileName, overwrite);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (!File.Exists(destFileName))
|
|
|
|
|
|
{
|
|
|
|
|
|
System.IO.File.Copy(sourceFileName, destFileName, overwrite);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Copies the directory.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sourceDirName">Name of the source dir.</param>
|
|
|
|
|
|
/// <param name="destDirName">Name of the dest dir.</param>
|
|
|
|
|
|
/// <param name="ignore">ignore file.</param>
|
|
|
|
|
|
/// <param name="overwrite">if set to <c>true</c> [overwrite].</param>
|
|
|
|
|
|
/// <param name="copySubDirs">if set to <c>true</c> [copy sub dirs].</param>
|
|
|
|
|
|
public static void CopyDirectory(string sourceDirName, string destDirName, Predicate<FileInfo> ignore = null, bool overwrite = true, bool copySubDirs = true)
|
|
|
|
|
|
{
|
|
|
|
|
|
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
|
|
|
|
|
|
|
|
|
|
|
|
if (!dir.Exists)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DirectoryInfo[] dirs = dir.GetDirectories();
|
|
|
|
|
|
|
|
|
|
|
|
if (!Directory.Exists(destDirName))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(destDirName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Get the files in the directory and copy them to the new location.
|
|
|
|
|
|
FileInfo[] files = dir.GetFiles();
|
|
|
|
|
|
foreach (FileInfo file in files)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ignore?.Invoke(file) == true)
|
|
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string temppath = Path.Combine(destDirName, file.Name);
|
|
|
|
|
|
|
|
|
|
|
|
if (overwrite)
|
|
|
|
|
|
{
|
|
|
|
|
|
file.CopyTo(temppath, overwrite);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (!File.Exists(temppath))
|
|
|
|
|
|
{
|
|
|
|
|
|
file.CopyTo(temppath, overwrite);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If copying subdirectories, copy them and their contents to new location.
|
|
|
|
|
|
if (copySubDirs)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (DirectoryInfo subdir in dirs)
|
|
|
|
|
|
{
|
|
|
|
|
|
string temppath = Path.Combine(destDirName, subdir.Name);
|
|
|
|
|
|
|
|
|
|
|
|
CopyDirectory(subdir.FullName, temppath, null, overwrite, copySubDirs);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Combins the path.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="list">The list.</param>
|
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
|
public static string CombinPath(IEnumerable<string> list)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Path.Combine(list.Select(o => o.Trim('\\', '/')).ToArray());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Combins the path.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="array">The array.</param>
|
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
|
public static string CombinPath(params string[] array)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Path.Combine(array.Select(o => o.Trim('\\', '/')).ToArray());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Safes the delete directory.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
|
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
|
|
|
|
|
|
public static bool SafeDeleteDirectory(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.Delete(path, true);
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception)
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Safes the delete file.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
|
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
|
|
|
|
|
|
public static bool SafeDeleteFile(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
File.Delete(path);
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception)
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the path with new extension.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
|
/// <param name="extension">The extension.</param>
|
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
|
public static string GetPathWithNewExtension(string path, string extension)
|
|
|
|
|
|
{
|
|
|
|
|
|
return CombinPath(
|
|
|
|
|
|
Path.GetDirectoryName(path),
|
|
|
|
|
|
Path.GetFileNameWithoutExtension(path) + extension);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// create dir
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="dir">the dir</param>
|
|
|
|
|
|
public static void CreateDirectory(string dir)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!Directory.Exists(dir))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(dir);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create Parent Directory
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="path">path</param>
|
|
|
|
|
|
public static void CreateParentDirectory(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
CreateDirectory(Path.GetDirectoryName(path));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|