Once your login session (running from your AppPool's login) has already mapped a drive to a server then you can't map another with different credentials, and in fact it makes no sense to map a second time because it remains mapped. In any event, you only
need to do this mapping once (per login session). If you use different username/passwords each time then you really should look for a different approach since you might have two requests at the same time trying to contend for this single mapped network drive
resource (if that makes sense).
Ok, then my guess is that maybe you have simultaneous requests trying to perform this action and one maps the drive and the second produces the error because there's already a mapped drive.
You have a timing issue. Best thing would be to protect this seciton of code with a monitor, but that introduces quite a bottleneck.
I don't think you can just do this once and leave it mapped and then keep a flag indicating that you already mapped... reason being is that your app pool might recycle and thus your flag would be lost.
The last idea might be to just try to do this mapping in App_Start and wrap a try/catch for the situation where it's already mapped. I don't really like this as an approach, but I don't know of a way to check if it's already mapped (other than trying to
access the share).
This is a tricky thing and certainly uncommon for server processes to do this -- mapping a network drive is typically for an interactive user. Perhaps consider alternatives? I'm not quite sure what the mapped drive is used for, so it's hard to provide concrete
suggestions.
Thanks for replying, there are files that are dumped on the mapped drive by some other processes and i need to copy those files on the local server and process the data and create a report using .net
Marked as answer by psm_8210 on Sep 06, 2012 07:06 AM
Thanks for replying, there are files that are dumped on the mapped drive by some other processes and i need to copy those files on the local server and process the data and create a report using .net
psm_8210
Member
9 Points
34 Posts
Access network folder using username and password
Apr 26, 2012 03:47 PM|LINK
Hi friends,
I need to access a network folder using the login details in order by to copy the files from it.
I am using the below code snippet inorder to do that;
string str = "NET USE " + directory + " /user:" + username + " " + password
Process process = new Process { StartInfo = new ProcessStartInfo { WindowStyle = ProcessWindowStyle.Hidden, FileName = "cmd.exe", Arguments = args } };
process.Start();
process.WaitForExit(5000);
process.Close();
but the problem is sometime i am able to access the network folder and sometimes it gives me a error saying Failed to login;
Not sure on this.
Did anyone else have faced the same issue??
BrockAllen
All-Star
27534 Points
4907 Posts
MVP
Re: Access network folder using username and password
Apr 26, 2012 03:56 PM|LINK
Once your login session (running from your AppPool's login) has already mapped a drive to a server then you can't map another with different credentials, and in fact it makes no sense to map a second time because it remains mapped. In any event, you only need to do this mapping once (per login session). If you use different username/passwords each time then you really should look for a different approach since you might have two requests at the same time trying to contend for this single mapped network drive resource (if that makes sense).
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
psm_8210
Member
9 Points
34 Posts
Re: Access network folder using username and password
Apr 26, 2012 04:06 PM|LINK
hi,
I did used the same username and password details everytime.
Also, the code for closing connection is
"NET USE " + directory + " /delete"
BrockAllen
All-Star
27534 Points
4907 Posts
MVP
Re: Access network folder using username and password
Apr 26, 2012 04:13 PM|LINK
Ok, then my guess is that maybe you have simultaneous requests trying to perform this action and one maps the drive and the second produces the error because there's already a mapped drive.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
psm_8210
Member
9 Points
34 Posts
Re: Access network folder using username and password
Apr 26, 2012 06:41 PM|LINK
Thanks,
that may be the case;
Is there a way to check if the drive is mapped or not using command line??
BrockAllen
All-Star
27534 Points
4907 Posts
MVP
Re: Access network folder using username and password
Apr 26, 2012 06:50 PM|LINK
You have a timing issue. Best thing would be to protect this seciton of code with a monitor, but that introduces quite a bottleneck.
I don't think you can just do this once and leave it mapped and then keep a flag indicating that you already mapped... reason being is that your app pool might recycle and thus your flag would be lost.
The last idea might be to just try to do this mapping in App_Start and wrap a try/catch for the situation where it's already mapped. I don't really like this as an approach, but I don't know of a way to check if it's already mapped (other than trying to access the share).
This is a tricky thing and certainly uncommon for server processes to do this -- mapping a network drive is typically for an interactive user. Perhaps consider alternatives? I'm not quite sure what the mapped drive is used for, so it's hard to provide concrete suggestions.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
psm_8210
Member
9 Points
34 Posts
Re: Access network folder using username and password
Apr 26, 2012 06:55 PM|LINK
Hi Brock,
Thanks for replying, there are files that are dumped on the mapped drive by some other processes and i need to copy those files on the local server and process the data and create a report using .net
BrockAllen
All-Star
27534 Points
4907 Posts
MVP
Re: Access network folder using username and password
Apr 26, 2012 06:58 PM|LINK
And this is done as part of an ASP.NET request?
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
psm_8210
Member
9 Points
34 Posts
Re: Access network folder using username and password
Apr 26, 2012 07:04 PM|LINK
Hi Brock,
Below things i am doing;
1. Created a console application which would be scheduled to run.
2. This console application would copy files if any from the mapped drive using username and password to the local drive.
3. The local files are processed and the data is added to the MS SQL server database tables.
4. These tables data are then used by some other process to generate reports - THIS POINT IS NOT IN SCOPE OF CONSOLE APPLICATION.
Its only until point 3 that the console application needs to implement.
psm_8210
Member
9 Points
34 Posts
Re: Access network folder using username and password
Apr 26, 2012 07:06 PM|LINK
Also,
I found a link http://stackoverflow.com/questions/1432213/copy-file-on-a-network-shared-drive which tells another approach to map drive