[C#] using System; using System.IO;class Test { public static void Main() { string path = @"c:\temp\MyTest.txt"; try { using (StreamWriter sw = File.CreateText(path)) {} string path2 = path + "temp"; // Ensure that the target does not exist. File.Delete(path2); // Copy the file. File.Copy(path, path2); Console.WriteLine("{0} was copied to {1}.", path, path2); // Delete the newly created file. File.Delete(path2); Console.WriteLine("{0} was successfully deleted.", path2); } catch (Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); } } }
string[] files = System.IO.Directory.GetFiles(@"C:\tmp"); for(int i = 0 ; i < files.Length ; i ++) { System.IO.File.Delete(files[i]); }
using System;
using System.IO;class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
try
{
using (StreamWriter sw = File.CreateText(path)) {}
string path2 = path + "temp"; // Ensure that the target does not exist.
File.Delete(path2); // Copy the file.
File.Copy(path, path2);
Console.WriteLine("{0} was copied to {1}.", path, path2); // Delete the newly created file.
File.Delete(path2);
Console.WriteLine("{0} was successfully deleted.", path2);
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}
for(int i = 0 ; i < files.Length ; i ++)
{
System.IO.File.Delete(files[i]);
}
private void del_dir(string path)
{
del_file(path);
string[] dirs = System.IO.Directory.GetDirectories(path);
for(int i = 0 ; i < dirs.Length ; i ++)
{
del_dir(dirs[i]);
System.IO.Directory.Delete(dirs[i]);
}
}
private void del_file(string path)
{
string[] files = System.IO.Directory.GetFiles(path);
for(int i = 0 ; i < files.Length ; i ++)
{
System.IO.File.Delete(files[i]);
}
}
test:
private void button2_Click(object sender, System.EventArgs e)
{
del_dir(@"c:\tmp");
}
private void Button2_Click(object sender, System.EventArgs e)
{
string path = @"D:\tmp";
if (File.Exists(path))
del_dir(path);
} private void del_dir(string path)
{
del_file(path);
string[] dirs = System.IO.Directory.GetDirectories(path);
for(int i = 0 ; i < dirs.Length ; i ++)
{
del_dir(dirs[i]);
System.IO.Directory.Delete(dirs[i]);
}
}
private void del_file(string path)
{
string[] files = System.IO.Directory.GetFiles(path);
for(int i = 0 ; i < files.Length ; i ++)
{
System.IO.File.Delete(files[i]);
}
}
for(int i = 0 ; i < files.Length ; i ++)
{
System.IO.File.Delete(files[i]);
}