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);
/// wèPATHÅêRs[·é
/// ³p[X
/// ÚWp[X
/// bool
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 GetDirList(string rootDir)
{
List retDir = new List();
string[] dirList = Directory.GetDirectories(rootDir);
retDir.AddRange(dirList);
foreach (string item in dirList)
{
retDir.AddRange(GetDirList(item));
}
return retDir;
}
/// wèPATHÅt@Cêðæ¾·é
/// ³p[X
/// t@Cê
/// bool
public static bool GetFileList(string hsSrcDir, ref List hoFileList)
{
return GetFileList(hsSrcDir, ref hoFileList, "*");
}
/// wèPATHÅt@Cêðæ¾·é
/// ³p[X
/// t@Cê
/// t@CÌ^Cv(á *.txtA*.sql)
/// bool
public static bool GetFileList(string hsSrcDir, ref List 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 { }
}
/// t@Cðí·é
/// t@C¼
/// bool
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;
}
}
/// t@CÌíÞÅí·é
/// p[X
/// t@CÌíÞ(á *.txtA*.sql)
/// bool
public static bool Delete(string hsSrcDir, string[] hsSearchPattern)
{
try
{
List hoList = new List();
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;
}
}
///
/// ÇÝæèêpðO·
///
///
///
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;
}
}
/// SPathí·é
/// p[X
/// bool
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;
}
}
/////
///// t@CÌRs[
/////
/////
//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);
//}
}
}