DirectoryInfo.GetFiles gets System.IO.IOException when file does not exist RRS feed

  • Question

  • We have an application developed in c# that do lots of fileprocessing. This application runs on Windows Server 2008 R2. We have tried both .NET Framework 4.5 and 4.0.
    The application runs the following code

    cSource String = "\\server1.domain\folder\subfolder";
    cFilename String = "NonExistingFile.txt';
    DirectoryInfo MyDirectoryInfo = new DirectoryInfo(cSource);
    FileInfo[] MyFileInfo = MyDirectoryInfo.GetFiles(cFilename, SearchOption.TopDirectoryOnly);
    Server1 is a windows server, We have tested both Windows server 2008 R2 and Windows Server 2003

    Our problem is that the last command, MyDirectoryInfo.Getfiles in some circumstances gets an exception when the searched file in sourcefolder do not exist.
    When the file exist we never get any problem. The exception is "System.IO.IOException: The specified network name is no longer available"
    We have checked fileoperations and file permissions on folders outside the application and we have made the account that runs the application local administrator on the server were we search for files so there is no security issue.

    We have figured out the following:
     - If application runs on a server in the same domain as the server in cSource we never get the exception.
     - If application runs on a server in domain1 and search for a file on a server in domain2 we get no exception
     - If application runs on a server in domain2 and search for a file on a server in domain1 we get this exception. We have tested both on 

    We run Mcafee as Virus protection on all servers and the Windows firewall is turned off on all servers

    Come someone hint us were to troubleshoot this.
    Which components/settings can be causing this exception?
    Is it the calling applications servers settings that blocks us or is it the server that we read files from that blocks us?

    Is it on the server that runs the application?
    Or is it on the server were we search for files?

    Thanks in advance


    Friday, May 16, 2014 11:14 AM


  • There are some files the are owned by the operating system that even an admin account will get an exception.  The only solution is the code I posted to get the folders recursively.


    Friday, May 16, 2014 3:06 PM

All replies

  • This is a common issue.  You need to write a recursive algorithm with an exception handler so when you don't have the credentials to access a file or folder youi will continue.  I have the code on my home computer and can supply it later today if you don't find it elsewhere.

    I found one version of my code at the following forum posting.  Use the April 2 code



    Friday, May 16, 2014 11:30 AM
  • Does this problem occurs occasionally or on each call of GetFiles()?

    Maybe this is an acceptable feature, so you can interpret this exception as “File does not exist”.

    Friday, May 16, 2014 2:07 PM
  • It occurs everytime when the file we search for does not exist.

    But (and this is the key point) it only occurs when we call from server on domain B to server on domain A.

    It does NOT occur when we call from server on domain A to server on domain B.

    And the user we run as is local administrator on both servers so there is no problem with missing credentials. We have doublechecked credentials from Windows Explorer.

    Thanks so far


    Friday, May 16, 2014 2:43 PM
  • There are some files the are owned by the operating system that even an admin account will get an exception.  The only solution is the code I posted to get the folders recursively.


    Friday, May 16, 2014 3:06 PM
  • Ultimately this has very little to do with the .NET Framework. GetFiles simply calls Win32 functions such as FindFirstFile and FindNextFile and reports back whatever errors those may return.

    Have you tried other methods to check the file's existence? File.Exists, File.GetLastWriteTime or even opening the file?

    Friday, May 16, 2014 4:01 PM
  • Yes, I agree that this question might be in the wrong forum, it should not be in .NET framework forum. I think it should be placed in some windows/network forum.
    Our question should be: 
    We do get exceptions when the searched file do not exist, but why do we only get these exceptions when we search from a server on domain B to a server on domain A.
    Why do we not get these exceptions when a server on domain A search for files on a server in domain B.
    Are there security policies that are related to this scenario or were should we troubleshoot?

    We get into this troubles when we are migrating applications from one sourcing partner (domain A) to another partner (domain B) and this problem involves  many applications and many lines of code, so unfortunate change to the code that has been working for several years at our first partner is not an option.
    For many months applications at new sourcing partner must be able to run against servers at old souring partner. /Roger
    Friday, May 23, 2014 10:49 AM
  • You files are probably on a file server and not on a PC itself.  The file server could be a unix machine and not windows.  The issues has to do with the file permissions and not the PC, although a unix machine may implement the permissions a little different than a window machine.  When searching between different machines the group policies of the intranet work you are using play a role in the permissions and may be reason for the exception.

    You may find useful information in the Control Panel : Administrative tools : Event Viewer on both the local machine, remote machine, and file server. 


    Friday, May 23, 2014 7:59 PM
  • As I tried to explain in the summery of the case all servers involved are windows server (no unix, linux, samba etc). The servers we try to read files from using Getfiles have been both windows server 2003 and windows server 2008 R2. The behaviour is the same of both operating systems at the searched server. It is when the searched server is on domain A and the application searches from domain B we get problems.

    Note that we have also tested the file permissions from application server on domain B to the server on domain A (the path that do not work using Getfiles) using windows explorer with the same account that runs the application on domain B. These tests show that we do no seem to have any restrictions. We can create files, rename other files, delete, open files, list folder contents. It is only when we do Getfiles for a file which do not exist from server on domain B to a server in domain A we get the exception.


    Friday, May 30, 2014 9:53 AM
  • Well, there's not much I can add here but anyway, maybe some of the following stuff will help:

    • the error comes from a Win32 function calls, it's Win32 error 64 - ERROR_NETNAME_DELETED
    • if you have a stack trace of the error you can look at the relevant .NET source code to see which Win32 function returned this error, I'd guess that this function is FindFirstFile
    • You can use a tool like Network Monitor to look at the network traffic. Network monitor has various protocol parsers so you may be able to see the SMB protocol commands that are sent over the network, if you're lucky and the traffic isn't encrypted
    Friday, May 30, 2014 11:33 AM