[英]C# How to Rename file after File.Copy without using File.Move
[英]How to Move File and Rename it in C#, after receiving it from the server?
我正在尝试移动和重命名从TCPserver收到的文件。
我的移动和重命名代码:
*//My sourcePath*
static string myServerfile = @"C:\Users\me\Documents\file_client\bin\Debug\test1.txt";
*//My destinationPath*
static string myFile = @"C:\test\inbox\JobStart.txt";
收到文件后,我这样做:
fs.Close ();
serverStream.Close ();
File.Move(myServerfile, myFile);
Console.WriteLine("Moved");
}
catch (Exception ex)
{
Console.WriteLine ("Cannot be DONE!");
}
但是当到达File.Move(myServerfile, myfile1);
时,它始终引发异常“无法完成” File.Move(myServerfile, myfile1);
我试过这个: Console.WriteLine(ex.ToString());
结果: System.IO.IOException:无法创建一个已经存在的文件。
我究竟做错了什么?
似乎您已经在目标文件夹中拥有JobStart.txt文件。
您可以尝试检查它是否存在,然后尝试替换或删除该文件然后移动。
if (File.Exists(myFile))
{
File.Delete(myFile);
}
File.Move(myServerfile, myFile);
尝试:
File.Move(@"C:\SAM.txt", @"C:\New Folder\SAM_newName.txt");
如果成功,则第一个文件将不再存在。 如果不成功,则该操作将终止-磁盘上的任何内容都不会更改。 我建议尝试使用此功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.