AI智能
改变未来

c#的文件路径使用问题

在C#代码中,定义关于路径的字符串变量时,字符串前要加@

[code]               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.ffi1.CopyTo(path2);  //文件从path复制到path2,同时删除路径path的文件richTextBox1.Text += String.Format(\"\\n{0} 复制到 {1}.\", path, path2);File.Delete(path);richTextBox1.Text += \"\\npath删除成功\";

或者将\\改为\\\\,

[code]string path = \"E:\\\\test\\\\testdata\\\\\" + str + \"\";

 

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » c#的文件路径使用问题