423,688 Members | 2,147 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,688 IT Pros & Developers. It's quick & easy.

An unexpected network error occurred in C#

P: 61
Hi all
I am using C#/.Net2.0

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

Expand|Select|Wrap|Line Numbers
  1. NetResource netRes = new NetResource();
  2.  
  3. netRes.scope = RESOURCE_GLOBALNET;
  4. netRes.type = RESOURCETYPE_DISK;
  5. netRes.displayType = RESOURCEDISPLAYTYPE_SHARE;
  6. netRes.usage = RESOURCEUSAGE_CONNECTABLE;
  7. netRes.remoteSharedName = shareName;
  8. netRes.localMappedDriveName = localMappedDrive;
  9.  
  10. int retcode = WNetAddConnection2A(ref netRes, pwd, usr, 0);
  11.  
  12. String[] fileColl = Directory.GetFiles(shareName);
  13.  
  14. // foreach file in fileColl
  15. DateTime dt = File.GetLastWriteTime(absFileName);//Here the exception is thrown.
  16.  
  17.  
The Exception is:
Type : System.IO.IOException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Message : An unexpected network error occurred.

Source : mscorlib
Help link :
Data : System.Collections.ListDictionaryInternal
TargetSite : Void WinIOError(Int32, System.String)
Stack Trace : at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.GetLastWriteTimeUtc(String path)
at System.IO.File.GetLastWriteTime(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.
Expand|Select|Wrap|Line Numbers
  1. class base
  2. {
  3.     // above code snippet
  4. }
  5.  
  6. class d1: base
  7. {
  8. }
  9. class d2: base
  10. {
  11. }
  12.  
One of childs is throwing this exception. Any guesses on this error is really appreciated.

Thanks
Ram
Dec 3 '08 #1
Share this Question
Share on Google+
7 Replies


Plater
Expert 5K+
P: 7,872
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.GetFiles(theUNCPath);
Dec 3 '08 #2

P: 61
@Plater
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..
Dec 4 '08 #3

Plater
Expert 5K+
P: 7,872
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)
Dec 4 '08 #4

P: 61
Yes Plater. The path is correct. Any other guesses on how to solve it!!!
Dec 5 '08 #5

Plater
Expert 5K+
P: 7,872
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\SomeRestrictedShare\
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?
Dec 5 '08 #6

P: 61
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\SomeShare 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\SomeShare\file.txt, and if it passes my filter, do copy it to my local pc. To summarize this,
Expand|Select|Wrap|Line Numbers
  1. 1) Connect to the remote share through WNetAddConnection2A
  2. by using  share, un, pwd.
  3. //Fetches the file names.
  4. 2) Use String[] fileColl = Directory.GetFiles();
  5. 3)
  6. foreach(String fn in fileColl)
  7. {
  8.  DateTime dtModTime = File.GetLastWriteTime(fn);
  9.  if(dtLastCollTime < dtModTime)
  10. {
  11.       // Do something
  12. }
  13. }
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.
Dec 6 '08 #7

Plater
Expert 5K+
P: 7,872
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.
Dec 8 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Browse more C# / C Sharp Questions on Bytes