File.Move throws exception
-
Hello all I am using WebMethod and File.Move But my problem is the file is being locked by the WebMethod and it cannot be moved again (I want to rename it, not move it) The exception thrown is "The process cannot access the file because it is being used by a another process" How do I solve this? I probably googled for half an hour Here is my code:
\[WebMethod\] public void EditFile(string FilePath, string NewName, string OldName) { File.Move(FilePath + OldName, FilePath + NewName); }
-
Hello all I am using WebMethod and File.Move But my problem is the file is being locked by the WebMethod and it cannot be moved again (I want to rename it, not move it) The exception thrown is "The process cannot access the file because it is being used by a another process" How do I solve this? I probably googled for half an hour Here is my code:
\[WebMethod\] public void EditFile(string FilePath, string NewName, string OldName) { File.Move(FilePath + OldName, FilePath + NewName); }
You cannot move or rename a file that is in use. You need to make sure whatever has the file open closes it first. What do you mean when you say "locked by the WebMethod". Is it your own code that has the file open? You just need to make sure you close the file before you try and rename it. (Post your code that opens/reads the file)
Simon
-
You cannot move or rename a file that is in use. You need to make sure whatever has the file open closes it first. What do you mean when you say "locked by the WebMethod". Is it your own code that has the file open? You just need to make sure you close the file before you try and rename it. (Post your code that opens/reads the file)
Simon
Im using jQuery ajax method to call the webservice $.ajax({ type: "POST", url: "fileManager/WebService.asmx/EditFile", contentType: "application/json; charset=utf-8", dataType: "json", data: "{'FilePath':'" + filePath + "', 'NewName':'" + newName.val() + "', 'OldName':'" + oldName.text() + "'}", }); The file is used in a tag No where else, hope you understand. It only works the first time I try it, the next it throws an error
-
Im using jQuery ajax method to call the webservice $.ajax({ type: "POST", url: "fileManager/WebService.asmx/EditFile", contentType: "application/json; charset=utf-8", dataType: "json", data: "{'FilePath':'" + filePath + "', 'NewName':'" + newName.val() + "', 'OldName':'" + oldName.text() + "'}", }); The file is used in a tag No where else, hope you understand. It only works the first time I try it, the next it throws an error
Casper Hansen wrote:
The file is used in a tag
Then you can't rename it. If a file is being used somewhere else, so you can't move or rename it until you stop using it. What is it you are trying to do?
Simon