using System;
|
using System.Collections.Generic;
|
using System.Text;
|
using System.IO;
|
|
namespace HotelPms.Share.IO
|
{
|
public class FileOperation
|
{
|
[System.Runtime.InteropServices.DllImport("shell32.dll")]
|
private static extern int ExtractIconEx(string lpszFile, int niconIndex, ref IntPtr phiconLarge, ref IntPtr phiconSmall, int nIcons);
|
|
/// <summary>wèPATHÅêRs[·é</summary>
|
/// <param name="hsSrcDir">³p[X</param>
|
/// <param name="hsDstDir">ÚWp[X</param>
|
/// <returns>bool</returns>
|
public static bool CopyTree(string hsSrcDir, string hsDstDir)
|
{
|
try
|
{
|
//ÚWp[XªÈ¯êÎAìéB
|
if (!Directory.Exists(hsDstDir))
|
{
|
Directory.CreateDirectory(hsDstDir);
|
}
|
|
//t@CðRs[
|
string[] wvFileArray = Directory.GetFiles(hsSrcDir);
|
if (wvFileArray.Length > 0)
|
{
|
foreach (string wsCurfile in wvFileArray)
|
{
|
string dstFile = hsDstDir + Path.DirectorySeparatorChar + Path.GetFileName(wsCurfile);
|
Delete(dstFile);
|
File.Copy(wsCurfile, dstFile, true);
|
}
|
}
|
|
//qtH_ðRs[
|
string wsCurSrcDir = null;
|
string wsCurFullDstDir = null;
|
|
string[] wvDirArray = Directory.GetDirectories(hsSrcDir);
|
|
if (wvDirArray.Length > 0)
|
{
|
foreach (string wsCurFullSrcDir in wvDirArray)
|
{
|
wsCurSrcDir = wsCurFullSrcDir.Substring(wsCurFullSrcDir.LastIndexOf(Path.DirectorySeparatorChar) + 1);
|
wsCurFullDstDir = hsDstDir + Path.DirectorySeparatorChar + wsCurSrcDir;
|
|
CopyTree(wsCurFullSrcDir, wsCurFullDstDir);
|
}
|
}
|
|
return true;
|
}
|
catch
|
{
|
return false;
|
}
|
|
}
|
|
public static List<string> GetDirList(string rootDir)
|
{
|
List<string> retDir = new List<string>();
|
string[] dirList = Directory.GetDirectories(rootDir);
|
retDir.AddRange(dirList);
|
|
foreach (string item in dirList)
|
{
|
retDir.AddRange(GetDirList(item));
|
}
|
|
return retDir;
|
}
|
|
/// <summary>wèPATHÅt@Cêðæ¾·é</summary>
|
/// <param name="hsSrcDir">³p[X</param>
|
/// <param name="hoFileList">t@Cê</param>
|
/// <returns>bool</returns>
|
public static bool GetFileList(string hsSrcDir, ref List<string> hoFileList)
|
{
|
return GetFileList(hsSrcDir, ref hoFileList, "*");
|
}
|
|
/// <summary>wèPATHÅt@Cêðæ¾·é</summary>
|
/// <param name="hsSrcDir">³p[X</param>
|
/// <param name="hoFileList">t@Cê</param>
|
/// <param name="hsSearchPattern">t@CÌ^Cv(á *.txtA*.sql)</param>
|
/// <returns>bool</returns>
|
public static bool GetFileList(string hsSrcDir, ref List<string> hoFileList, string hsSearchPattern)
|
{
|
try
|
{
|
string[] wvFileArray = Directory.GetFiles(hsSrcDir, hsSearchPattern);
|
if (wvFileArray.Length > 0)
|
{
|
foreach (string wsCurfile in wvFileArray)
|
{
|
hoFileList.Add(wsCurfile);
|
}
|
}
|
|
string[] wvDirArray = Directory.GetDirectories(hsSrcDir);
|
|
if (wvDirArray.Length > 0)
|
{
|
foreach (string wsCurFullSrcDir in wvDirArray)
|
{
|
GetFileList(wsCurFullSrcDir, ref hoFileList, hsSearchPattern);
|
}
|
}
|
|
return true;
|
}
|
catch
|
{
|
return false;
|
}
|
}
|
|
public static void DeleteFile(string fileName)
|
{
|
try
|
{
|
FileInfo fi = new FileInfo(fileName);
|
if ((fi.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
|
{
|
fi.Attributes = FileAttributes.Normal;
|
}
|
|
File.Delete(fileName);
|
}
|
catch { }
|
}
|
|
/// <summary>t@Cðí·é</summary>
|
/// <param name="fileName">t@C¼</param>
|
/// <returns>bool</returns>
|
public static bool Delete(string fileName)
|
{
|
try
|
{
|
FileInfo fi = new FileInfo(fileName);
|
if ((fi.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
|
{
|
fi.Attributes = FileAttributes.Normal;
|
}
|
|
File.Delete(fileName);
|
|
return true;
|
}
|
catch
|
{
|
return false;
|
}
|
}
|
|
/// <summary>t@CÌíÞÅí·é</summary>
|
/// <param name="hsSrcDir">p[X</param>
|
/// <param name="hsSearchPattern">t@CÌíÞ(á *.txtA*.sql)</</param>
|
/// <returns>bool</returns>
|
public static bool Delete(string hsSrcDir, string[] hsSearchPattern)
|
{
|
try
|
{
|
List<string> hoList = new List<string>();
|
for (int i = 0; i < hsSearchPattern.Length; i++)
|
{
|
GetFileList(hsSrcDir, ref hoList, hsSearchPattern[i]);
|
}
|
|
for (int i = 0; i < hoList.Count; i++)
|
{
|
FileInfo fi = new FileInfo(hoList[i].ToString());
|
if ((fi.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
|
{
|
fi.Attributes = FileAttributes.Normal;
|
}
|
|
File.Delete(hoList[i].ToString());
|
}
|
|
hoList.Clear();
|
|
return true;
|
}
|
catch
|
{
|
return false;
|
}
|
}
|
|
/// <summary>
|
/// ÇÝæèêpðO·
|
/// </summary>
|
/// <param name="filePath"></param>
|
/// <returns></returns>
|
public static bool UnReadOnly(string filePath)
|
{
|
try
|
{
|
FileInfo fi = new FileInfo(filePath);
|
if ((fi.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
|
{
|
fi.Attributes = FileAttributes.Normal;
|
}
|
return true;
|
}
|
catch
|
{
|
return false;
|
}
|
}
|
|
/// <summary>SPathí·é</summary>
|
/// <param name="aimpath">p[X</param>
|
/// <returns>bool</returns>
|
public static bool DeleteDir(string aimpath)
|
{
|
try
|
{
|
if (aimpath[aimpath.Length - 1] != Path.DirectorySeparatorChar)
|
{
|
aimpath += Path.DirectorySeparatorChar;
|
}
|
string[] filelist = Directory.GetFileSystemEntries(aimpath);
|
foreach (string file in filelist)
|
{
|
if (Directory.Exists(file))
|
{
|
DeleteDir(aimpath + Path.GetFileName(file));
|
}
|
else
|
{
|
string filePath = aimpath + Path.GetFileName(file);
|
UnReadOnly(filePath);
|
File.Delete(filePath);
|
}
|
}
|
Directory.Delete(aimpath, true);
|
return true;
|
}
|
catch
|
{
|
return false;
|
}
|
}
|
|
///// <summary>
|
///// t@CÌRs[
|
///// </summary>
|
///// <param name="fileList"></param>
|
//public static void CopyToClipboard(System.Collections.Specialized.StringCollection fileList)
|
//{
|
// //System.Collections.Specialized.StringCollection strcoll = new System.Collections.Specialized.StringCollection();
|
// //strcoll.Add(System.Windows.Forms.Application.ExecutablePath);
|
// //strcoll.Add(Application.StartupPath + @"\GoogleJapaneseInputSetup.exe");
|
// Clipboard.SetFileDropList(fileList);
|
//}
|
}
|
}
|