473,666 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

An unexpected network error occurred in C#

61 New Member
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:

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.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.
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 14135
Plater
7,872 Recognized Expert Expert
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) ;
Dec 3 '08 #2
Ramk
61 New Member
@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 Recognized Expert Expert
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 New Member
Yes Plater. The path is correct. Any other guesses on how to solve it!!!
Dec 5 '08 #5
Plater
7,872 Recognized Expert Expert
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?
Dec 5 '08 #6
Ramk
61 New Member
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,
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 Recognized Expert Expert
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
2970
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 fullpath = " http://10.60.93.51/TempFile "; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(fullpath); //req.KeepAlive = false; HttpWebResponse res; StreamReader sr; string strResult = "";
5
24749
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 on a receive. We are using .NET v1.1.
0
1419
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 page of the application makes a call to a service and is painted successfully. Navigation to any other application page, which invokes the same service throws the error, "Underlying connection was closed. An unexpected error occurred on receive"....
4
20478
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 webservice operates on SSL via an "https://" url. they also gave me a .PFX certificate which ive installed via window's MMC utility, into the "Computer account"'s Personal store; as indicated by articles id found. i then export a .CER to the filesystem...
11
4213
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 As String
3
3311
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: “Unexpected end of file while parsing Name has occurred” isbeing thrown. Following is the part o xml I am trying to read: <CHECK_ITEM_OUT>
2
5874
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 connection was closed: An unexpected error occurred on a send. The only way I have been able to make consecutive web method invocations work is by overriding GetRequest and disabling Keep-Alive. The IIS Keep-Alive setting is enabled for 120...
1
8836
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) Client app: SmartClient - WinForms (.NET 2.0) I trust all server cetrificates - at the start of client application I call:
0
522
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 occurred on a send" I have used the same application in One of the institute where I am doing some course. It works fine. I encounter the following issue on the Visual Studio startup page(2005 and 2008). The current news channel might not be...
0
8356
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8871
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8551
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.