Can you move a file/folder across a network share in .NET?
Trying to do this:
Dim originalPath As String = "\\comp1\c$\target"
Dim destinationPath As String = "\\comp2\c$\target"
If Directory.Exists(path) Then
Directory.Move(originalPath, destinationPath)
End If
But it fail开发者_开发知识库s. Is there another way I can achieve the same effect?
As Barry said, Directory.Move() does not work across volumes - two differnt computers (as shown in your example) certainly do not qualify as the same volume (as might be the case with two different shares which at the end point to the same volume on the same computer - but I'm not sure here).
First do a Directory.CreateDirectory(targetpath), then copy all the files (using Directory.GetFiles(); recursively creating subdirectories as required), when all that was successfull do a Directory.Delete() on the sourcepath.
You may want to search the web for an existing implementation.
Note that this is not an atomic operation. So you might want to consider the case when only part of the source directory could be copied, leaving parts in destination.
You can't move directory to a different volume.
http://msdn.microsoft.com/en-us/library/system.io.directory.move.aspx
Also, you are checking if path
exists - is this a typo as path doesn't exist in the code you have provided?
Take a look at MSDN documentation. I would try trapping the execption to see why it is failing.
I'm going to answer... yes. You can copy/move files/folders through ASP.NET.
If it fails, it typically generates an exception that tells you more about the error. I've seen these errors with file/folder copies in the past.
- Inadequate permissions to read/modify source folder and/or target folder
- One of the network paths doesn't resolve (check for typos, try to fully qualify)
- Source folder supports characters that target folder doesn't support (i.e. copying from Windows to SharePoint)
We can help you better with the exception details.
精彩评论