Jump to content

File.Move throws exception

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Dumpen

Dumpen

    Newbie

  • Members
  • PipPip
  • 14 posts
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);

    }


#2
cdg10620

cdg10620

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 389 posts
Are you using another method to access the path of the file or get the file name? There has to be something else going on before this method is called in order for that error to be thrown. Another thing you may want to try is see if you can use File.Copy and copy the old file to the new name and then use File.Delete to remove the old one. See if that throws an error. It's not the best method but you can use that for debugging to see if it gives the same error message.

Let me know if you need any further assistance. :D
-CDG10620
Software Developer

#3
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
I am sure that you left a FileReader or FileStream instance object left open in your code.
use the

FileStream fs=new FileStream();
fs.close(); //use this to close the file handles