0

part of a console application I have is built to delete an unwanted order file out of a directory on a server we use. This part of the code works fine when I run it off of my local machine, but when we run it on the server it self (it will be a scheduled task), it no longer deletes the file.

try
{
    System.IO.File.Delete(@"\\ServerName\ProductionFileShare\Orderfiles\FileDir\" + fileName);
}
catch (System.IO.IOException e)
{
    Console.WriteLine(e.Message);
    return;
}

The filename is just the storeorder.txt.

As I said, this works just fine on my local computer, but does not delete the file when we run this program from the server itself. Any ideas?

UPDATE: The exception message is: The process cannot access the file '\ServerName\ProductionFileShare\Orderfiles\FileDir\storeorder_07062014_16-25-0.txt' because it is being used by another process.

This is strange to me, since it works on my computer. I will add some garbage collection and see what happens.

UPDATE #2: Apparently that fixed it! That was an unexpected solution. Thanks to everyone for taking the time to comment, I really appreciate it.

psoshmo
  • 1,490
  • 10
  • 19
  • And the error message in the exception is? – Steve Jul 07 '14 at 20:15
  • 2
    The local system account probably doesn't have the privs to delete the file. The exception should point you that way. – 48klocs Jul 07 '14 at 20:16
  • The account under which the task is run may not have the privilege to access the network share (even if it's on the very same machine). Maybe a local filesystem path (d:\orderfiles\...) will work. But more details will help solving the problem. – derpirscher Jul 07 '14 at 20:24
  • Assuming you're running under the same credentials on your local machine as on the server, another possibility is user account control (UAC) being enabled on the server. If that's the case: http://stackoverflow.com/questions/3925065/correct-way-to-deal-with-uac-in-c-sharp – JohnLBevan Jul 07 '14 at 20:31

1 Answers1

0

I've had this problem before, where I simply moved to the admin share (\\server\c$\path\file.txt), or changed the path to a local path, and all was good.

It should be mentioned that there are both file permissions, and permissions to access the share itself, the later is likely where the problem lies.

If changing the path helps (and you do not have easy access to fix the share), then you could make it a configuration setting, and use a different configuration at each location.

Greg
  • 2,410
  • 21
  • 26