can a service map a network share to a drive letter

G

Guest

Is it possible to write a .net Service that would map a remote share to a
drive letter.

The reasons:
Mistake 1
A long time ago someone wrote lots and lots of programs in vb6 that require
driver letter type drive mappings.

Mistake 2
They would log on to the server with a shared account name and then screen
lock the server, so the drive maps would stay.

The security guys recently said bad bad bad to Mistake 2. So its either
rewrite lots of vb6 programs or find another work around. I was hoping to
map the drives with a service; I am not sure if this is possible though.
 
D

Donn Felker

Here ya go, I've used this before.

System.Diagnostics.Process p =
System.Diagnostics.Process.Start("net.exe", @"use H: \\server\share");
p.WaitForExit();
DriveInfo info = new DriveInfo("H");
Console.WriteLine(info.AvailableFreeSpace);
p = System.Diagnostics.Process.Start("net.exe", @"use H: /DELETE");
p.WaitForExit();
 
D

Donn Felker

This has worked for me in the past.

System.Diagnostics.Process p =
System.Diagnostics.Process.Start("net.exe", @"use H: \\server\share");
p.WaitForExit();
DriveInfo info = new DriveInfo("H");
Console.WriteLine(info.AvailableFreeSpace);
p = System.Diagnostics.Process.Start("net.exe", @"use H: /
DELETE");
p.WaitForExit();
 
D

Donn Felker

This has worked for me in the past.

System.Diagnostics.Process p =
System.Diagnostics.Process.Start("net.exe", @"use H: \\server\share");
p.WaitForExit();
DriveInfo info = new DriveInfo("H");
Console.WriteLine(info.AvailableFreeSpace);
p = System.Diagnostics.Process.Start("net.exe", @"use H: /
DELETE");
p.WaitForExit();
 
J

Jeffrey Tan[MSFT]

Hi Chuck,

Yes, Windows provided the network management APIs which can be used to
establish a LAN session to the remote share directory. Also, it has the
function to map the remote share as a local drive. However, .Net Framework
did not encapsulate these APIs.

Also, Windows provided the net.exe console tool which encapsulates the LAN
manager network management APIs. Donn has provided the C# regarding how to
use Process class to invoke the net.exe function from .Net app. You may
also choose to p/invoke the LAN manager network management APIs directly in
.Net application. I have written the sample C# code in the link below:
http://groups.google.com/group/microsoft.public.platformsdk.security/msg/063
d7b08b29e5642?hl=zh-CN&

In the sample code, I did not specify the local drive for mapping, however,
it is easy to modify it a bit to set the drive name in USE_INFO_2.ui1_local
field.

My sample code also demonstrates how to use a specific username/password
pair to establish the LAN session instead of using the interactive user
credential.

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Thanks,
I wasn't sure if I could do this in a service.
I wasn't sure if you could map drives when their is no logged on user.
 
J

Jeffrey Tan[MSFT]

Hi Chuck,

Thank you for the feedback.

This approach will work in Windows Service. I have just created a sample
project and used Donn's code snippet for testing, it works well by printing
the drive free space. From principle perspective, this established LAN
session and drive letter will be setup in the calling process logon
session. So this LAN session is established in user wide. This means that
the LAN session you established in Windows Service will be invisible to the
interactive user because they are in different logon session in the machine.

Hope this is clear to you. Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

cubaman

Is it possible to write a .net Service that would map a remote share to a
drive letter.

The reasons:
Mistake 1
A long time ago someone wrote lots and lots of programs in vb6 that require
driver letter type drive mappings.

Mistake 2
They would log on to the server with a shared account name and then screen
lock the server, so the drive maps would stay.

The security guys recently said bad bad bad to Mistake 2. So its either
rewrite lots of vb6 programs or find another work around. I was hoping to
map the drives with a service; I am not sure if this is possible though.

Well, I had see net admins doing taht with a bat script that just runs
when a user log in. Maybe write a service for this is not the best
aproach, as long as you can get the same results with a simple bat
file.
Best regards.
Oscar Acosta
 
G

Guest

Jeffrey, thanks for explanation on the LAN session.

Is their a way to create a service to map the network drives, where the
mapped drives would be available to programs that are scheduled to run under
the Task Scheduler?
 
J

Jeffrey Tan[MSFT]

Hi Chuck,

Thank you for the feedback.

This question goes a step further in complexity. The answer depends on what
account your mapping drive Windows Service runs and how do you use the Task
Scheduler(what account does it run). Actually, the key point is determining
whether these two processes(windows service and the process stared by Task
Scheduler) reside in the same logon session.

If your Windows Service runs under Local System, Network Service or Local
System account, there will be one to one mapping between the account and
logon session. So once the process is started by Task Scheduler as the same
account, it will see the drive mapping. Below test confirms my statement:
1. I configure my Windows Service runs under Local System and run it to
setup the drive mapping
2. I am using the command below to run "cmd.exe" one minute
later(substitute 15:51 with your time):
"at 15:51 /interactive /every:friday cmd.exe"
3. The cmd.exe will run under Local System account one minute later. Just
type "H:" in the cmd.exe, you can see the cmd.exe can jump into "H:" drive
without any problem. This confirms that the cmd.exe can see the drive
mapping.

If the Windows Service and the process started by Task Scheduler run under
different accounts, they definitely can not drive mapping each other.

But note: if the Windows Service runs under interactive user account, even
the process started by Task Scheduler run this interactive user account
also, they can not see each other. This is because Windows Service has a
special logon session allocation policy for normal user account. Windows
Service with interactive user account will still be assigned a different
logon session as interactive logon session.

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top