Mapping Network Drive in C#

  • Thread starter Thread starter greatbarrier86
  • Start date Start date
G

greatbarrier86

Sorry about that previous one. I pressed enter too early.

How does one go about mapping a network drive in C#. i know you use
MapNetworkDrive in scripting languages, but i'm not sure how to do it in C#.
It doesnt seem like C$ would use MapNetworkDrive.
 
Sorry about that previous one. I pressed enter too early.

How does one go about mapping a network drive in C#. i know you use
MapNetworkDrive in scripting languages, but i'm not sure how to do it in C#.
It doesnt seem like C$ would use MapNetworkDrive.

I would use the Process class to run the NET USE command prompt
command.

An enterprising .NET programmer would write it as a class method so
the process of mapping a network drive would be simple to implement in
any application they may work on.
 
Sorry about that previous one. I pressed enter too early.

How does one go about mapping a network drive in C#. i know you use
MapNetworkDrive in scripting languages, but i'm not sure how to do it in C#.
It doesnt seem like C$ would use MapNetworkDrive.

Hi,

You have two options, you either call "net use" using the Process
class (with ShellExecute=true)
or you find the win32 API call and invoke it using p/invoke

I prefer the later.
 
message
Sorry about that previous one. I pressed enter too early.

How does one go about mapping a network drive in C#. i know you use
MapNetworkDrive in scripting languages, but i'm not sure how to do it in
C#.
It doesnt seem like C$ would use MapNetworkDrive.

Hi,

You have two options, you either call "net use" using the Process
class (with ShellExecute=true)
or you find the win32 API call and invoke it using p/invoke

I prefer the later.


Or much better, don't map network drives, but use UNC paths instead.

Willy.
 
message


Hi,

You have two options, you either call "net use" using the Process
class (with ShellExecute=true)
or you find the win32 API call and invoke it using p/invoke

I prefer the later.

As do I. I'm reasonably sure there is an example of this on
codeproject.com
Or much better, don't map network drives, but use UNC paths instead.

There are good reasons to map drives, the least of which is older
applications that can't handle
UNC paths.

Matt
 
message


Hi,

You have two options, you either call "net use" using the Process
class (with ShellExecute=true)
or you find the win32 API call and invoke it using p/invoke

I prefer the later.

As do I. I'm reasonably sure there is an example of this on
codeproject.com
Or much better, don't map network drives, but use UNC paths instead.

There are good reasons to map drives, the least of which is older
applications that can't handle
UNC paths.


This may be right when talking about existing older application, however,
the OP is asking about the possibility to "map a drive" from within his C#
code, this is not an older application, it's a new application.
Mapping a drive in new developments, turns the application into an "old
application", and this will become again an argument (excuse) in the
near/far future. People who mapped drives in the past will probably continue
to do so in the future, unless MS removes this "feature".


Willy.
 
Mapping a drive in new developments, turns the application into an "old
application", and this will become again an argument (excuse)  in the
near/far future. People who mapped drives in the past will probably continue
to do so in the future, unless MS removes this "feature".

Willy.- Hide quoted text -

Agreed, they should remove it, or at least not promoting it, I do not
think it will happen anytime soon though
 
message
Mapping a drive in new developments, turns the application into an "old
application", and this will become again an argument (excuse) in the
near/far future. People who mapped drives in the past will probably
continue
to do so in the future, unless MS removes this "feature".

Willy.- Hide quoted text -

Agreed, they should remove it, or at least not promoting it, I do not
think it will happen anytime soon though


Don't know if MS promotes this, anyway they had the idea to deprecate some
of Net API's ten years ago (W2K development), but you can imagine the storm
of "I need this for backward compatibility" reactions. Now, that there is no
direct mapping in the Framework, people continue to think they need a
*mapped drive letter* and now go through the pain of PInvoke to achieve the
"I need a drive so I can do some IO" syndrome.

Willy.
 
As do I. I'm reasonably sure there is an example of this on
codeproject.com




There are good reasons to map drives, the least of which is older
applications that can't handle
UNC paths.

This may be right when talking about existing older application, however,
the OP is asking about the possibility to "map a drive" from within his C#
code, this is not an older application, it's a new application.
Mapping a drive in new developments, turns the application into an "old
application", and this will become again an argument (excuse)  in the
near/far future. People who mapped drives in the past will probably continue
to do so in the future, unless MS removes this "feature".

Willy.

I think you missed my point. Imagine that you have data you need to
"feed" to
an application that can't read UNC. You map a network drive, write the
data there
and allow the application to use it.

Matt
 
As do I. I'm reasonably sure there is an example of this on
codeproject.com




There are good reasons to map drives, the least of which is older
applications that can't handle
UNC paths.

This may be right when talking about existing older application, however,
the OP is asking about the possibility to "map a drive" from within his C#
code, this is not an older application, it's a new application.
Mapping a drive in new developments, turns the application into an "old
application", and this will become again an argument (excuse) in the
near/far future. People who mapped drives in the past will probably
continue
to do so in the future, unless MS removes this "feature".

Willy.

I think you missed my point. Imagine that you have data you need to
"feed" to
an application that can't read UNC. You map a network drive, write the
data there
and allow the application to use it.

Matt


You don't need to map a network drive to write the data to a shared
resource, just use the UNC path to write the data to, it's the
"UNC-unaware" application's duty to map the drive, not the other
application's. Don't force new applications to inherit the bad habits of
network drive mappings.

Willy.
 
greatbarrier86 said:
How does one go about mapping a network drive in C#. i know you use
MapNetworkDrive in scripting languages, but i'm not sure how to do it in C#.
It doesnt seem like C$ would use MapNetworkDrive.

You can use Win32 API:

using System;
using System.Runtime.InteropServices;

namespace E
{
public class Program
{
public enum ResourceScope
{
RESOURCE_CONNECTED = 1,
RESOURCE_GLOBALNET,
RESOURCE_REMEMBERED,
RESOURCE_RECENT,
RESOURCE_CONTEXT
};
public enum ResourceType
{
RESOURCETYPE_ANY,
RESOURCETYPE_DISK,
RESOURCETYPE_PRINT,
RESOURCETYPE_RESERVED
};
public enum ResourceUsage
{
RESOURCEUSAGE_CONNECTABLE = 0x00000001,
RESOURCEUSAGE_CONTAINER = 0x00000002,
RESOURCEUSAGE_NOLOCALDEVICE = 0x00000004,
RESOURCEUSAGE_SIBLING = 0x00000008,
RESOURCEUSAGE_ATTACHED = 0x00000010
};
public enum ResourceDisplayType
{
RESOURCEDISPLAYTYPE_GENERIC,
RESOURCEDISPLAYTYPE_DOMAIN,
RESOURCEDISPLAYTYPE_SERVER,
RESOURCEDISPLAYTYPE_SHARE,
RESOURCEDISPLAYTYPE_FILE,
RESOURCEDISPLAYTYPE_GROUP,
RESOURCEDISPLAYTYPE_NETWORK,
RESOURCEDISPLAYTYPE_ROOT,
RESOURCEDISPLAYTYPE_SHAREADMIN,
RESOURCEDISPLAYTYPE_DIRECTORY,
RESOURCEDISPLAYTYPE_TREE,
RESOURCEDISPLAYTYPE_NDSCONTAINER
};
[StructLayout(LayoutKind.Sequential)]
public struct NETRESOURCE
{
public ResourceScope dwScope;
public ResourceType dwType;
public ResourceDisplayType dwDisplayType;
public ResourceUsage dwUsage;
public string lpLocalName;
public string lpRemoteName;
public string lpComment;
public string lpProvider;
};
[DllImport("mpr.dll")]
public static extern int WNetAddConnection2(ref NETRESOURCE
netResource, string password, string username, int flags);
public static void Main(string[] args)
{
NETRESOURCE res = new NETRESOURCE();
res.dwType = ResourceType.RESOURCETYPE_DISK;
res.lpLocalName = "Z:";
res.lpRemoteName = @"\\ARNE\PCBKUP";
int stat = WNetAddConnection2(ref res, null, null, 0);
Console.WriteLine(stat);
Console.ReadKey();
}
}
}

or you can actually use MapNetworkDive (.NET works with COM):

using System;
using IWshRuntimeLibrary;

namespace E
{
public class Program
{
public static void Main(string[] args)
{
IWshNetwork wshn = new WshNetworkClass();
object missing = Type.Missing;
wshn.MapNetworkDrive(@"Z:", @"\\ARNE\PCBKUP", ref missing, ref
missing, ref missing);
Console.ReadKey();
}
}
}

or you can find some existing code:

http://www.codeproject.com/KB/system/mapnetdrive.aspx

Arne
 
Arne's code looks like a time saver. The discussion about using UNC
names is interesting to me because I have not found any way of accessing
disk space information for drives that are not mapped to a drive letter.
The obvious way is to assign each drive to a drive letter and get the
info from that. But that uses the old DOS drive letter system, and that
can't be good, as discussed.
From an application programmer's point of view it seems inexplicable
that there is no apparent way of getting the info, which makes me think
there must be a way of doing it if only I knew where to look. Does
anybody know how to get drive space (and cluster size etc.) info from a
UNC specified drive?
 
Back
Top