locked
Access denied while moving a file from code, but i am able to move the file manually. RRS feed

  • Question

  • File.Move(fileName, archivePath + " fileTime.csv");

    file name is the path of the source file, i have full access over it. it is a server machine, archivepath is destination path.

    this is the part  where i get access denied. when i try to move the file manually, i am able to move it, but when i try to move from the code, it is giving access denied error.

    can any one please help me on this? very urgent please

    Monday, December 9, 2013 11:19 AM

Answers

  • Hi Rohit,

    Welcomes you to MSDN forums..

    it looks like you may not have access rights to either the source path or destination path. 
    If the paths are valid and still you face this problem, I suggest you to refer to the below code- where it sets the access permissions of all files to normal; and then proceed with your copy.
     
    //For checking if the destination folder already contains any files/ a file with same name

    //For checking if the destination folder already contains any files/ a file with same name
     
    foreach (string newPath in Directory.GetFiles( , "*.*",SearchOption.AllDirectories))
    {
    var file = new FileInfo(newPath);
    file.Attributes = FileAttributes.Normal;
    file.Move(fileName, archivePath + " fileTime.csv");
    //File.Delete(newPath);  if you want to delete the existing file.. else not req.
    }

    I hope it will helps to you..

    By,

    A Pathfinder.

    Jo Swa(P Elayaraja)

    http://www.ijcstjournal.org/


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful"

    Monday, December 9, 2013 3:40 PM

All replies

  • I forgot to say that, archive path is also a server where i have full access on destination.

    this is the error : Access denied to the path.

    Monday, December 9, 2013 11:22 AM
  • Hi Rohit,

    Welcomes you to MSDN forums..

    it looks like you may not have access rights to either the source path or destination path. 
    If the paths are valid and still you face this problem, I suggest you to refer to the below code- where it sets the access permissions of all files to normal; and then proceed with your copy.
     
    //For checking if the destination folder already contains any files/ a file with same name

    //For checking if the destination folder already contains any files/ a file with same name
     
    foreach (string newPath in Directory.GetFiles( , "*.*",SearchOption.AllDirectories))
    {
    var file = new FileInfo(newPath);
    file.Attributes = FileAttributes.Normal;
    file.Move(fileName, archivePath + " fileTime.csv");
    //File.Delete(newPath);  if you want to delete the existing file.. else not req.
    }

    I hope it will helps to you..

    By,

    A Pathfinder.

    Jo Swa(P Elayaraja)

    http://www.ijcstjournal.org/


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful"

    Monday, December 9, 2013 3:40 PM