Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. File.Move throws exception

File.Move throws exception

Scheduled Pinned Locked Moved C#
questionhelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Casper Hansen
    wrote on last edited by
    #1

    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);
    }
    
    S 1 Reply Last reply
    0
    • C Casper Hansen

      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);
      }
      
      S Offline
      S Offline
      Simon P Stevens
      wrote on last edited by
      #2

      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

      C 1 Reply Last reply
      0
      • S Simon P Stevens

        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

        C Offline
        C Offline
        Casper Hansen
        wrote on last edited by
        #3

        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

        S 1 Reply Last reply
        0
        • C Casper Hansen

          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

          S Offline
          S Offline
          Simon P Stevens
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups