473,396 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

An unexpected network error occurred in C#

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
7 14096
Plater
7,872 Expert 4TB
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
Ramk
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
7,872 Expert 4TB
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
Ramk
61
Yes Plater. The path is correct. Any other guesses on how to solve it!!!
Dec 5 '08 #5
Plater
7,872 Expert 4TB
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
Ramk
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
7,872 Expert 4TB
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

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

Similar topics

1
by: Angel | last post by:
I'm trying to connect to a fixed IP address (eg. http://10.60.903.50/TempFile) in order to retrieve one accii line of text in TempFile. I try to read the information with this code: string...
5
by: Vijayakrishna Pondala | last post by:
Hi, We are using the following error randomly, when accessing a webservice method/servlet hosted on JBoss application server: The underlying connection was closed: An unexpected error occurred...
0
by: tharika | last post by:
Hi, We have an ASP.NET web application hosted on a development server, that invokes web services hosted on an offshore server in India, both being on the same intranet domain. The default...
4
by: Matthew.DelVecchio | last post by:
hello, i am developing an ASP.NET web app that consumes a 3rd party vendor webservice. it is my first one so while ive done my homework, im not an expert on the matter. our partner's...
11
by: MLH | last post by:
Private Sub ButtonP_Click() On Error GoTo Err_ButtonP_Click Dim ThisForm As String ThisForm = Me.Name Exit_ButtonP_Click: Exit Sub Err_ButtonP_Click: Dim r As String, k As String, Message3...
3
by: Anup Daware | last post by:
Hi Group, I am facing a strange problem here: I am trying to read xml response from a servlet using XmlTextWriter. I am able to read the read half of the xml and suddenly an exception:...
2
by: Scott McFadden | last post by:
When I invoke two web service methods sequentially with no delay, the first web method invocation goes smooth while the 2nd one generates the dredded: System.Net.WebException: The underlying...
1
by: Mrozik | last post by:
Hi! I have a problem - after deploying application in client environment, on some client machines occured error. Environment: App server: ASP.NET WebService/Win 2003/ secured SSL (.NET 2.0)...
0
by: =?Utf-8?B?c2dPcmNoaWQ=?= | last post by:
I am creating a Windows forms application which uses WebRequest to download RSS feed. The application generates the following error. "The underlying connection was closed: An unexpected error...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.