|
|
|
C# WinApp If แบบหลายๆเงื่อนไขแบบนี้ถูกไม๊ครับ if (delFolder != DelOption.Nothing || (delFolder == DelOption.DelEmpty && filesystem.GetFolder(dirs.FullName).Size <= 0) || delFolder == DelOption.DelAll) |
|
|
|
|
|
|
|
มีใครพอจะลองเอาไปรันให้หน่อยได้ไม๊ครับว่าโค๊ดบ้านๆผมใช้งานได้ไม๊
Code (C#)
Code (C#)
public static void DeleteFileAndFolder(string pathSources,DelOption delFolder = DelOption.DelEmpty,DelOption delFile = DelOption.DelEmpty,string delExt = "",bool msg = true )
{
new System.Threading.Thread(RenameFileByFolder).Start(new object[] { pathSources,delFolder,delFile,delExt ,msg});
}
void DeleteFileAndFolder(object obj)
{
string path = (string)((object[])obj)[0];
DelOption delFolder = (DelOption)((object[])obj)[1];
DelOption delFile = (DelOption)((object[])obj)[2];
string delExt = (string)((object[])obj)[3];
bool msg = (bool)((object[])obj)[4];
IWshRuntimeLibrary.FileSystemObject filesystem = new IWshRuntimeLibrary.FileSystemObject();
System.Collections.Generic.Stack<string> stack = new System.Collections.Generic.Stack<string>();
stack.Push(path);
while ((stack.Count > 0))
{
string dir = stack.Pop();
System.IO.DirectoryInfo dirs = new System.IO.DirectoryInfo(dir);
// delete folder ลบทั้งหมด หรือ แค่โฟลเดอร์ว่าง
if (delFolder != DelOption.Nothing && ((delFolder == DelOption.DelEmpty && filesystem.GetFolder(dirs.FullName).Size <= 0) || delFolder == DelOption.DelAll))
{
try
{
dirs.Delete(true);
}
//ถ้าลบไม่ได้ก็ย้ายมันซะ
catch {dirs.MoveTo(System.IO.Path.GetTempPath() + "\\" + String.Format("{0:ddMMyyyyHHmmss}", DateTime.Now) + dirs.Name); }
System.Threading.Thread.Sleep(10);
}
if (System.IO.Directory.Exists(dir))
{
foreach (string file in System.IO.Directory.GetFiles(dir, "*.*"))
{
// delete Foile ลบทั้งหมด หรือ แค่ Foile ว่าง หรือ Foile ที่มีคำที่ระบุอยู่ในชื่อไฟล์
if (delFile != DelOption.Nothing && ((delFile == DelOption.DelEmpty && filesystem.GetFile(file).Size <= 0) || delFile == DelOption.DelAll || delExt != ""))
{
System.IO.FileInfo _file = new System.IO.FileInfo(file);
try
{
_file.Delete();
}
catch { _file.MoveTo(System.IO.Path.GetTempPath() + "\\" + String.Format("{0:ddMMyyyyHHmmss}", DateTime.Now) + _file.Name); }
}
System.Threading.Thread.Sleep(10);
}
string directoryName = null;
foreach (string directoryName_loopVariable in System.IO.Directory.GetDirectories(dir))
{
directoryName = directoryName_loopVariable;
stack.Push(directoryName);
}
}
}
MessageBox.Show("Process Complete...");
}
|
|
|
|
|
Date :
2015-07-16 11:17:42 |
By :
lamaka.tor |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|