Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File.WriteAllLines Silent Failure

Tags:

c#

file

file-io

I've stumbled upon a somewhat unusual issue with File.WriteAllLines.

I have code that looks like this

File.WriteAllLines(filename, data);
bool exists = File.Exists(filename);

The problem is that sometimes file writing fails, but does not raise an exception, and the code thinks the file exists when it doesn't.

  • The file is in a network location.
  • The file name is Database.lock. Does a lock extension mean anything to the OS?
  • Exists returns true, but the file is simply not there. No exception is raised.
  • Calling Exists from a separate process returns false.
  • Calling Process.Start(filename) results in an error (not a code exception, just the OS saying it can't find the file).
  • The local machine is running Windows 7.
  • The remote machine is running Windows XP.

How can I debug what's going on here?

Update

Following David's advice, I watched the process using procmon.exe.

This is the result: http://i.imgur.com/IBz6Ujt.png

You'll notice there's a lot of things going on repetitively, which I don't fully understand, and at the end, the file is reported to have been written successfully.

Solved

Thanks to Patrick's suggestion, I discovered that due to a code path I hadn't taken into consideration, the file was getting immediately deleted in a different segment of code. Sorry for wasting everyone's time. I am relieved though that it's just me being thoughtless, instead of unforeseeable network issues.

like image 547
Rotem Avatar asked Nov 11 '22 20:11

Rotem


1 Answers

This could be a permissions issue. File.Exists will return false if you don't have read permissions for the file. It could be that you are maybe running your code to create the file from Visual Studio and it has admin privileges while you are running LINQPad with other permissions that don't have read access to that location.

like image 154
PatrickV Avatar answered Nov 15 '22 13:11

PatrickV