/// <summary>
/// IO 操作类
/// </summary>
public class IOHelper
{
/// <summary>
/// 创建目录
/// </summary>
/// <param name="dirName"></param>
/// <param name="Name"></param>
/// <returns></returns>
public static bool CreateDirectory(string dirName, string Name)
{
try
{
DirectoryInfo dirInfo = Directory.CreateDirectory(dirName + @"\" + Name);
return dirInfo != null;
}
catch (Exception ex)
{
ServiceManager.Instance.LoggingService.Debug("创建目录失败" + ex.Message);
}
return false;
}
/// <summary>
/// 输入路径名称获取绝对路径名称,如果绝对路径存在,直接返回原来的名称
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static string GetAbsoluteFileName(string fileName)
{
if (System.IO.File.Exists(fileName))
{
return fileName;
}
return System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + @"\" + fileName;
}
/// <summary>
/// 获取绝对目录名
/// </summary>
/// <param name="pathName"></param>
/// <returns></returns>
public static string GetAbsoluteDirectoryName(string pathName)
{
if (System.IO.Directory.Exists(pathName))
{
return pathName;
}
return System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + @"\" + pathName;
}
/// <summary>
/// 连接两个文件
/// </summary>
/// <param name="pathFirst"></param>
/// <param name="pathSecond"></param>
/// <returns></returns>
public static string CombinePathName(string pathFirst, string pathSecond)
{
if (pathFirst.Length < 1)
{
return pathSecond;
}
if (pathSecond.Length < 1)
{
return pathFirst;
}
if (pathFirst[pathFirst.Length - 1] == '\\' ||
pathFirst[pathFirst.Length - 1] == '/' ||
pathSecond[0] == '\\' ||
pathSecond[0] == '/'
)
{
return pathFirst + pathSecond;
}
return pathFirst + "\\" + pathSecond;
}
/// <summary>
/// 移动文件
/// </summary>
/// <param name="sourceFileName"></param>
/// <param name="destFileName"></param>
/// <returns></returns>
public static bool MoveFile(string sourceFileName, string destFileName)
{
bool bOk = true;
try
{
File.Move(sourceFileName, destFileName);
}
catch (Exception ex)
{
ServiceManager.Instance.LoggingService.Debug(ex);
bOk = false;
}
return bOk;
}
/// <summary>
/// 拷贝文件的方法
/// </summary>
/// <param name="srcFileName"></param>
/// <param name="targetFileName"></param>
/// <param name="overwrite"></param>
/// <returns></returns>
public static bool CopyFile(string srcFileName, string targetFileName, bool overwrite)
{
if (!File.Exists(srcFileName))
{
return false;
}
try
{
File.Copy(srcFileName, targetFileName, overwrite);
return true;
}
catch (Exception ex)
{
ServiceManager.Instance.LoggingService.Debug("拷贝文件出现异常:[" + ex.ToString());
}
return false;
}
/// <summary>
/// 删除目录
/// </summary>
/// <param name="directoryName"></param>
/// <returns></returns>
public static bool DeleteDirectory(string directoryName)
{
if (!Directory.Exists(directoryName))
{
return true;
}
try
{
Directory.Delete(directoryName, true);
}
catch (Exception ex)
{
ServiceManager.Instance.LoggingService.Debug("删除目录失败:" + ex.Message);
return false;
}
return true;
}
/// <summary>
/// 删除文件
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static bool DeleteFile(string fileName)
{
if (!File.Exists(fileName))
{
return true;
}
try
{
File.Delete(fileName);
}
catch (Exception ex)
{
ServiceManager.Instance.LoggingService.Debug("删除文件失败:" + ex.Message);
return false;
}
return true;
}
/// <summary>
/// 拷贝文件或文件夹到剪贴板上
/// </summary>
/// <param name="pathArray"></param>
/// <returns></returns>
public static bool CopyFile2Clipboard(string[] pathArray)
{
if (pathArray == null)
{
return false;
}
StringCollection pathCollection = new StringCollection();
foreach (string path in pathArray)
{
if (File.Exists(path) || Directory.Exists(path))
{
pathCollection.Add(path);
}
else
{
return false;
}
}
try
{
Clipboard.SetFileDropList(pathCollection);
}
catch (Exception ex)
{
ServiceManager.Instance.LoggingService.Debug("复制出现错误:" + ex.Message);
return false;
}
return true;
}
}
/// IO 操作类
/// </summary>
public class IOHelper
{
/// <summary>
/// 创建目录
/// </summary>
/// <param name="dirName"></param>
/// <param name="Name"></param>
/// <returns></returns>
public static bool CreateDirectory(string dirName, string Name)
{
try
{
DirectoryInfo dirInfo = Directory.CreateDirectory(dirName + @"\" + Name);
return dirInfo != null;
}
catch (Exception ex)
{
ServiceManager.Instance.LoggingService.Debug("创建目录失败" + ex.Message);
}
return false;
}
/// <summary>
/// 输入路径名称获取绝对路径名称,如果绝对路径存在,直接返回原来的名称
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static string GetAbsoluteFileName(string fileName)
{
if (System.IO.File.Exists(fileName))
{
return fileName;
}
return System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + @"\" + fileName;
}
/// <summary>
/// 获取绝对目录名
/// </summary>
/// <param name="pathName"></param>
/// <returns></returns>
public static string GetAbsoluteDirectoryName(string pathName)
{
if (System.IO.Directory.Exists(pathName))
{
return pathName;
}
return System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + @"\" + pathName;
}
/// <summary>
/// 连接两个文件
/// </summary>
/// <param name="pathFirst"></param>
/// <param name="pathSecond"></param>
/// <returns></returns>
public static string CombinePathName(string pathFirst, string pathSecond)
{
if (pathFirst.Length < 1)
{
return pathSecond;
}
if (pathSecond.Length < 1)
{
return pathFirst;
}
if (pathFirst[pathFirst.Length - 1] == '\\' ||
pathFirst[pathFirst.Length - 1] == '/' ||
pathSecond[0] == '\\' ||
pathSecond[0] == '/'
)
{
return pathFirst + pathSecond;
}
return pathFirst + "\\" + pathSecond;
}
/// <summary>
/// 移动文件
/// </summary>
/// <param name="sourceFileName"></param>
/// <param name="destFileName"></param>
/// <returns></returns>
public static bool MoveFile(string sourceFileName, string destFileName)
{
bool bOk = true;
try
{
File.Move(sourceFileName, destFileName);
}
catch (Exception ex)
{
ServiceManager.Instance.LoggingService.Debug(ex);
bOk = false;
}
return bOk;
}
/// <summary>
/// 拷贝文件的方法
/// </summary>
/// <param name="srcFileName"></param>
/// <param name="targetFileName"></param>
/// <param name="overwrite"></param>
/// <returns></returns>
public static bool CopyFile(string srcFileName, string targetFileName, bool overwrite)
{
if (!File.Exists(srcFileName))
{
return false;
}
try
{
File.Copy(srcFileName, targetFileName, overwrite);
return true;
}
catch (Exception ex)
{
ServiceManager.Instance.LoggingService.Debug("拷贝文件出现异常:[" + ex.ToString());
}
return false;
}
/// <summary>
/// 删除目录
/// </summary>
/// <param name="directoryName"></param>
/// <returns></returns>
public static bool DeleteDirectory(string directoryName)
{
if (!Directory.Exists(directoryName))
{
return true;
}
try
{
Directory.Delete(directoryName, true);
}
catch (Exception ex)
{
ServiceManager.Instance.LoggingService.Debug("删除目录失败:" + ex.Message);
return false;
}
return true;
}
/// <summary>
/// 删除文件
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static bool DeleteFile(string fileName)
{
if (!File.Exists(fileName))
{
return true;
}
try
{
File.Delete(fileName);
}
catch (Exception ex)
{
ServiceManager.Instance.LoggingService.Debug("删除文件失败:" + ex.Message);
return false;
}
return true;
}
/// <summary>
/// 拷贝文件或文件夹到剪贴板上
/// </summary>
/// <param name="pathArray"></param>
/// <returns></returns>
public static bool CopyFile2Clipboard(string[] pathArray)
{
if (pathArray == null)
{
return false;
}
StringCollection pathCollection = new StringCollection();
foreach (string path in pathArray)
{
if (File.Exists(path) || Directory.Exists(path))
{
pathCollection.Add(path);
}
else
{
return false;
}
}
try
{
Clipboard.SetFileDropList(pathCollection);
}
catch (Exception ex)
{
ServiceManager.Instance.LoggingService.Debug("复制出现错误:" + ex.Message);
return false;
}
return true;
}
}