在C#代码中,定义关于路径的字符串变量时,字符串前要加@
if (!Directory.Exists(@"E:\test\backdata"))
{
Directory.CreateDirectory(@"E:\test\backdata");
}
string path = @"E:\test\testdata\" + str + "";
string path2 = @"E:\test\backdata\" + str + "";
FileInfo fi1 = new FileInfo(path);
FileInfo fi2 = new FileInfo(path2);
try
{
// Create the source file.
// using (FileStream fs = fi1.Create()) { }
//Ensure that the target file does not exist.
if (File.Exists(path2))
{
fi2.Delete();
}
//Copy the file.f
fi1.CopyTo(path2); //文件从path复制到path2,同时删除路径path的文件
richTextBox1.Text += String.Format("\n{0} 复制到 {1}.", path, path2);
File.Delete(path);
richTextBox1.Text += "\npath删除成功";
或者将\改为\\,
string path = "E:\\test\\testdata\\" + str + "";