vb.net 复制移动重命名文件的更好方法,Visual Basic (VS 2012 V11)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16431590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Better Way to Copy Move Rename File, Visual Basic (VS 2012 V11)
提问by user2348797
I want to copy, rename, and move a file to a new location. According to http://msdn.microsoft.com/en-us/library/36xbexyf(v=vs.90).aspxthe following code should do so:
我想复制、重命名文件并将其移动到新位置。根据http://msdn.microsoft.com/en-us/library/36xbexyf(v=vs.90).aspx下面的代码应该这样做:
This example copies the file Test.txt to the directory TestFiles2 and renames it NewFile.txt.
本示例将文件 Test.txt 复制到目录 TestFiles2 并将其重命名为 NewFile.txt。
My.Computer.FileSystem.CopyFile _
("C:\UserFiles\TestFiles\test.txt", _
"C:\UserFiles\TestFiles2", "NewFile.txt", FileIO.UICancelOption.DoNothing)
However, when I type this code it only sees the "NewFile.txt" parameter as the Boolean parameter that handles overwrites. I believe this is a mistake on the websites part.
但是,当我键入此代码时,它只会将“NewFile.txt”参数视为处理覆盖的布尔参数。我相信这是网站部分的错误。
If I can't use "My.Computer.FileSystem.CopyFile" (unless I am doing something wrong) to copy rename and move a file, is there a better way then this:
如果我不能使用“My.Computer.FileSystem.CopyFile”(除非我做错了什么)来复制重命名和移动文件,那么有没有更好的方法:
' Rename the file to be copied the name you want it to be in the new location
My.Computer.FileSystem.RenameFile("C:\OriginalFile.txt", "OriginalFileTemporaryName.txt")
' Copy the file to the new location and overwrite if it exists there
My.Computer.FileSystem.CopyFile ("C:\OriginalFileTemporaryName.txt", "C:\UserFiles\TestFiles2", True)
' Rename the original file back to it's original name
My.Computer.FileSystem.RenameFile("C:\OriginalFileTemporaryName.txt", "OriginalFile.txt")
The problem I have with the above is that I might end up renaming the original file a temporary name that already exists in the original files location. I want to avoid that. I also do not want to rename the copied file in the destination folder because I do not see an option to force an overwrite for "My.Computer.FileSystem.RenameFile"
我在上面遇到的问题是我最终可能会将原始文件重命名为原始文件位置中已经存在的临时名称。我想避免这种情况。我也不想重命名目标文件夹中复制的文件,因为我没有看到强制覆盖“My.Computer.FileSystem.RenameFile”的选项
Is this even a half decent solution? Is there not a better way to do this?
这甚至是一个不错的解决方案吗? 没有更好的方法来做到这一点吗?
回答by Dmitry Kirsanov
I think you have syntax error. You are trying to give the target file name as two parameters - directory name and file name, when you should give both as one string.
我认为你有语法错误。您试图将目标文件名作为两个参数提供 - 目录名和文件名,而您应该将两者作为一个字符串提供。
My.Computer.FileSystem.CopyFile ("C:\UserFiles\TestFiles\test.txt", "C:\UserFiles\TestFiles2\NewFile.txt")
Like that. Third parameter could be boolean (True/False) - whether or not you wouldd like to overwrite the target file if it exists.
像那样。第三个参数可以是布尔值 (True/False) - 如果目标文件存在,您是否要覆盖它。
If you want to first check whether the file exists, take a look at System.IO.File class, it has "Exists" method, which accepts file name and returns boolean. It has also methods to manipulate files which you can use instead of FileSystem class, although there is no performance advantage.
如果要先检查文件是否存在,请查看 System.IO.File 类,它具有“Exists”方法,该方法接受文件名并返回布尔值。它还具有操作文件的方法,您可以使用这些方法代替 FileSystem 类,尽管没有性能优势。
回答by michaelb958--GoFundMonica
It certainly looks like a mistake on the website. That shouldn't stop us, though; just put the directory and filename into the same parameter:
这当然看起来像是网站上的错误。不过,这不应该阻止我们。只需将目录和文件名放入同一个参数中:
My.Computer.FileSystem.CopyFile("C:\UserFiles\TestFiles\test.txt", "C:\UserFiles\TestFiles2\NewFile.txt", FileIO.UICancelOption.DoNothing)
(If you want to force an overwrite, change the third parameter to True
. I don't know that you can overwrite and pass CancelOption.DoNothing
in the same call.)
(如果要强制覆盖,将第三个参数更改为True
。我不知道您可以覆盖并传入CancelOption.DoNothing
同一个调用。)