简体   繁体   中英

Access to the Path is denied in windows service

I am using windows service to import data from excel to a database. after importing I want to move that file to backup folder.

private void moveFileToBackUpFolder(string filename, string _inputFilePath)
{
    try
    {
        WriteToFile("moveFileToBackUpFolder  " + filename);

        string sourcePath = _inputFilePath;
        string targetPath = _inputFilePath + "\\Backup Files";

        string sourceFile = Path.Combine(sourcePath, filename);
        string destFile = Path.Combine(targetPath, filename);

        if (!Directory.Exists(targetPath))
        {
            Directory.CreateDirectory(targetPath);
        }

        File.Move(sourceFile, destFile);

    }
    catch (Exception ex)
    {
        WriteToFile("moveFileToBackUpFolder execption  " + ex.Message);
    }
}

but i am getting access to the path is denied. Same code i am using in windows form and it is working. I am using windows server 2016 .

Can anyone help me tosolve this issue.

Please check which user is running the windows service ? Go to Services > Properties (Your service) > Log On . Does that user have access to the path ?

Add the user which has access and you should be good.

Check that directory or file has proper permission. Below given reason stop to move file from one location to another.

  1. The caller does not have the required permission.
  2. The file is an file that is in use.
  3. Path is a directory.
  4. Path specified read-only file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM