// ***********************************************************************
// Assembly : Construction
// Author : zjx
// Created : 11-13-2020
//
// Last Modified By : zjx
// Last Modified On : 11-13-2020
// ***********************************************************************
//
// Copyright (c) jindongfang. All rights reserved.
//
//
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Fk.Utils
{
///
/// Class IO.
///
public static class WorkAreaDirectoryIO
{
///
/// Copies the file.
///
/// Name of the source file.
/// Name of the dest file.
/// if set to true [overwrite].
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);
}
}
///
/// Copies the directory.
///
/// Name of the source dir.
/// Name of the dest dir.
/// ignore file.
/// if set to true [overwrite].
/// if set to true [copy sub dirs].
public static void CopyDirectory(string sourceDirName, string destDirName, Predicate 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);
}
}
}
///
/// Combins the path.
///
/// The list.
/// System.String.
public static string CombinPath(IEnumerable list)
{
return Path.Combine(list.Select(o => o.Trim('\\', '/')).ToArray());
}
///
/// Combins the path.
///
/// The array.
/// System.String.
public static string CombinPath(params string[] array)
{
return Path.Combine(array.Select(o => o.Trim('\\', '/')).ToArray());
}
///
/// Safes the delete directory.
///
/// The path.
/// true if XXXX, false otherwise.
public static bool SafeDeleteDirectory(string path)
{
try
{
Directory.Delete(path, true);
return true;
}
catch (Exception)
{
return false;
}
}
///
/// Safes the delete file.
///
/// The path.
/// true if XXXX, false otherwise.
public static bool SafeDeleteFile(string path)
{
try
{
File.Delete(path);
return true;
}
catch (Exception)
{
return false;
}
}
///
/// Gets the path with new extension.
///
/// The path.
/// The extension.
/// System.String.
public static string GetPathWithNewExtension(string path, string extension)
{
return CombinPath(
Path.GetDirectoryName(path),
Path.GetFileNameWithoutExtension(path) + extension);
}
///
/// create dir
///
/// the dir
public static void CreateDirectory(string dir)
{
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
}
///
/// Create Parent Directory
///
/// path
public static void CreateParentDirectory(string path)
{
CreateDirectory(Path.GetDirectoryName(path));
}
}
}