-2

I'm encountering a very strange bug which appears only when compiling in Release mode, while in Debug mode the code runs perfectly. Moreover, the bug is encountered only in one machine (a user reported this).

This is the stack trace:

System.IO.FileNotFoundException: Unable to find file 'C:\Users...\FileName.txt'. File name: 'C:\Users...\FileName.txt' in System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) in System.IO.FileInfo.get_Length() in PatcherNET4.FileHandler.LocalFile.get_Size() in PatcherNET4.FileHandler.CachedFile.IsLocalValid(LocalFile file) in PatcherNET4.FileHandler.FileManager.<>c__DisplayClassd.b__9(CachedFile file) in System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable1 source, Func2 predicate) in PatcherNET4.FileHandler.FileManager.RemoveLocalFiles() in PatcherNET4.FileHandler.FileManager.DownloadMissingFiles() in System.Threading.ThreadHelper.ThreadStart_Context(Object state) in System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) in System.Threading.ThreadHelper.ThreadStart()

And this is the code:

// CachedFile
...
public bool IsLocalValid(LocalFile file)
{
    var checkName = file.Name == Name;
    var checkSize = file.Size == Size;
    var checkLastWrite = file.LastWriteTime == LastWriteTime;
    return checkName && checkSize && checkLastWrite;
}
...

//LocalFile
...
public uint Size
{
    get
    {
        _info.Refresh();
        return (uint)_info.Length;
    }
}
...

How is this possible? I can assure you that there is no difference in the interested pieces of code between the release and debug mode. I don't really know what to do, this is probably the strangest bug I've ever seen.

Niccolò Campolungo
  • 11,824
  • 4
  • 32
  • 39

2 Answers2

0

put FileName.txt in the Unable to find file 'C:\Users...\(here) directory

HackerMan
  • 904
  • 7
  • 11
  • @LightStyle check the file name brother System.IO.FileNotFoundException <----look carefully! – HackerMan Apr 14 '14 at 20:14
  • Already did. I'm not that stupid, don't worry. The file is there, and the program is not recognizing it. I also thought it could be a problem of the path (it contained a space inside of it), I made the user change it and it didn't work either. – Niccolò Campolungo Apr 14 '14 at 20:15
  • Well, it's possible. But how do you explain that this "stupid program" works on 200+ machines without errors and it doesn't work **only in release mode** on a single machine? – Niccolò Campolungo Apr 14 '14 at 20:17
  • It's possible. But I can't yet explain myself how it is possibly different the same program in Debug and Release mode. – Niccolò Campolungo Apr 14 '14 at 20:21
  • @LightStyle if you use **current directory** in debug mode...you should use **absolute** location in release mode....ok – HackerMan Apr 14 '14 at 20:25
0

The first thing I'd try is to verify the file exists at runtime and go from there.

if (!File.Exists(fileName))
{
    // do something
}

If the file doesn't exist (I believe you when you say it's missing) then consider the alternatives. Has the file been deleted already? I notice in the stacktrace there's RemoveLocalFiles, so perhaps it's already been run. Maybe a user pressed a button twice? race condition. Separate thread? User is logged in on two machines and has roaming profiles. Or something like that.

Regardless of whether it's a bug or not, it's better to be defensive and include a check to make sure the file exists (or catch the exception if that's your preference).

stevieg
  • 652
  • 4
  • 14