An unexpected network error occurred in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ramk
    New Member
    • Nov 2008
    • 61

    An unexpected network error occurred in C#

    Hi all
    I am using C#/.Net2.0

    I am connecting to a remote shared folder from my C# code using WNetAddConnecti on2A without mapping to the local drive.
    A code snippet is:

    Code:
    NetResource netRes = new NetResource();
    
    netRes.scope = RESOURCE_GLOBALNET;
    netRes.type = RESOURCETYPE_DISK;
    netRes.displayType = RESOURCEDISPLAYTYPE_SHARE;
    netRes.usage = RESOURCEUSAGE_CONNECTABLE;
    netRes.remoteSharedName = shareName;
    netRes.localMappedDriveName = localMappedDrive;
    
    int retcode = WNetAddConnection2A(ref netRes, pwd, usr, 0);
    
    String[] fileColl = Directory.GetFiles(shareName);
    
    // foreach file in fileColl
    DateTime dt = File.GetLastWriteTime(absFileName);//Here the exception is thrown.
    The Exception is:
    Type : System.IO.IOExc eption, mscorlib, Version=2.0.0.0 , Culture=neutral , PublicKeyToken= b77a5c561934e08 9
    Message : An unexpected network error occurred.

    Source : mscorlib
    Help link :
    Data : System.Collecti ons.ListDiction aryInternal
    TargetSite : Void WinIOError(Int3 2, System.String)
    Stack Trace : at System.IO.__Err or.WinIOError(I nt32 errorCode, String maybeFullPath)
    at System.IO.File. GetLastWriteTim eUtc(String path)
    at System.IO.File. GetLastWriteTim e(String path)
    The above block of code is in base class, and its been used by two different childs which are called from two different threads. A pseudo class is given below.
    Code:
    class base
    {
        // above code snippet
    }
    
    class d1: base
    {
    }
    class d2: base
    {
    }
    One of childs is throwing this exception. Any guesses on this error is really appreciated.

    Thanks
    Ram
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    So you are just trying to get a
    \\somecomputer\ someshare\
    Without mapping it to like drive z:\ ?
    Why not just put that UNC path into your code

    String[] fileColl = Directory.GetFi les(theUNCPath) ;

    Comment

    • Ramk
      New Member
      • Nov 2008
      • 61

      #3
      Originally posted by Plater
      So you are just trying to get a
      \\somecomputer\ someshare\
      Without mapping it to like drive z:\ ?
      Why not just put that UNC path into your code

      String[] fileColl = Directory.GetFi les(theUNCPath) ;
      This will not work if the UNCPath is protected by user credentials.A slight
      catch is that, when you save the password while accessing the UNCPath through the Connect dialog, the above API can fetch the files. In my case, the share is always protected by credentials. Also, my code has to connect to the folder automatically without manual intervention. I believe, C# doesn't provide direct way to access the remote shared folder operations with credentials..

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Well I am pretty sure there is a way (not positive), but you would either need to prompt or hardcode them in.

        Are you sure you are giving the path correctly? (ending with a \ or etc)

        Comment

        • Ramk
          New Member
          • Nov 2008
          • 61

          #5
          Yes Plater. The path is correct. Any other guesses on how to solve it!!!

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            I am still a little confused.

            1)I have a network share that is username/password restricted.
            2)I go to manually open up the directory with \\SomeComputer\ SomeRestrictedS hare\
            3)It prompts for username/password. I supply correct values and it lets me in.
            4)From now on I can use File.Open() with the UNC path

            Are you saying you can skip #2/#3 and use you win32API call for #4 and it throws up the prompt box?

            Comment

            • Ramk
              New Member
              • Nov 2008
              • 61

              #7
              To reduce your confusion, Im repeating my tasks.
              You are correct. I don't want to pop-up the connect dlg. I need to connect to \\SomeComp\Some Share through C# Program & copy the required files to my local machine. Both computers are in WORKGROUP. To prepare the required file list, I have to access the last modified time of the \\SomeComp\Some Share\file.txt, and if it passes my filter, do copy it to my local pc. To summarize this,
              Code:
              1) Connect to the remote share through WNetAddConnection2A
              by using  share, un, pwd.
              //Fetches the file names.
              2) Use String[] fileColl = Directory.GetFiles();
              3)
              foreach(String fn in fileColl)
              {
               DateTime dtModTime = File.GetLastWriteTime(fn);
               if(dtLastCollTime < dtModTime)
              {
                    // Do something
              }
              }
              One doubt is that, how much time Windows XP will maintain one network sesssion which we open through the API call. Is there any limit on this? Of course, my code will not take much time...hardly around 12sec.Also, my code uses 3connections to 3diffrent sub folders of the SomeShare and these are the maximum connections used by my code at one point of time.
              Also, recently I noticed that, even if we use single session also, the same fancy error is recurring. In single connection case, it some times fails.
              But, in multi connection cases, one of the 3connections is failing always.

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                So if the user has never supplied credentials to that location that is password protected, your API call is able to avoid the credentials requirement and access the files anyway?
                If you open up a console window and type "NET USE", do your access entries show up?
                When you try to navigate to a share and are successfull (ie if it requires credentials or just lets you in) an entry is made on that list.
                I believe they normally stay there as long as the user stays logged in.

                Comment

                Working...