if (File.Exists(Server.MapPath(path))) { File.Delete(Server.MapPath(path)); } 如果文本文档被打开,需要先关闭,SendMessage(handle,WM_CLOSE,NULL,NULL)
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()); } } } 创建并删除文件!
if (File.Exists(Server.MapPath(path)))
{
File.Delete(Server.MapPath(path));
} 如果文本文档被打开,需要先关闭,SendMessage(handle,WM_CLOSE,NULL,NULL)
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());
}
}
}
创建并删除文件!
private string strFileName = "";
public Form1()
{
InitializeComponent();
}
private bool CheckSourse()
{
if (strFileName == "" || !File.Exists(strFileName))
{
MessageBox.Show("请选择文件!");
return false;
}
else
return true;
}private void btnDel_Click(object sender, EventArgs e)
{
if (!CheckSourse())
return; try
{
File.Delete(strFileName);
}
catch (Exception ex)
{
throw ex;
} }