By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,194 Members | 1,694 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,194 IT Pros & Developers. It's quick & easy.

Mapping Network Drive in C#

P: n/a
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.
Jun 27 '08 #1
Share this Question
Share on Google+
10 Replies


P: n/a
On Apr 23, 12:47*pm, greatbarrier86
<greatbarrie...@discussions.microsoft.comwrote:
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.
Jun 27 '08 #2

P: n/a
On Apr 23, 12:47*pm, greatbarrier86
<greatbarrie...@discussions.microsoft.comwrote:
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.
Jun 27 '08 #3

P: n/a
"Ignacio Machin ( .NET/ C# MVP )" <ig************@gmail.comwrote in
message
news:3d**********************************@a22g2000 hsc.googlegroups.com...
On Apr 23, 12:47 pm, greatbarrier86
<greatbarrie...@discussions.microsoft.comwrote:
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.

Jun 27 '08 #4

P: n/a
On Apr 23, 3:58*pm, "Willy Denoyette [MVP]"
<willy.denoye...@telenet.bewrote:
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac...@gmail.comwrote in
messagenews:3d**********************************@a 22g2000hsc.googlegroups.com...
On Apr 23, 12:47 pm, greatbarrier86

<greatbarrie...@discussions.microsoft.comwrote:
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.
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
>
Willy.
Jun 27 '08 #5

P: n/a
"Matt" <ma********@sprynet.comwrote in message
news:0a**********************************@k37g2000 hsf.googlegroups.com...
On Apr 23, 3:58 pm, "Willy Denoyette [MVP]"
<willy.denoye...@telenet.bewrote:
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac...@gmail.comwrote in
messagenews:3d**********************************@a 22g2000hsc.googlegroups.com...
On Apr 23, 12:47 pm, greatbarrier86

<greatbarrie...@discussions.microsoft.comwrote:
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.
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.

Jun 27 '08 #6

P: n/a
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
Jun 27 '08 #7

P: n/a
"Ignacio Machin ( .NET/ C# MVP )" <ig************@gmail.comwrote in
message
news:9c**********************************@d1g2000h sg.googlegroups.com...
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.

Jun 27 '08 #8

P: n/a
On Apr 24, 3:47*am, "Willy Denoyette [MVP]"
<willy.denoye...@telenet.bewrote:
"Matt" <matttel...@sprynet.comwrote in message

news:0a**********************************@k37g2000 hsf.googlegroups.com...
On Apr 23, 3:58 pm, "Willy Denoyette [MVP]"

<willy.denoye...@telenet.bewrote:
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac...@gmail.comwrote in
messagenews:3d**********************************@a 22g2000hsc.googlegroups.com...
On Apr 23, 12:47 pm, greatbarrier86
<greatbarrie...@discussions.microsoft.comwrote:
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.

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.
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

Jun 27 '08 #9

P: n/a
"Matt" <ma********@sprynet.comwrote in message
news:23**********************************@t63g2000 hsf.googlegroups.com...
On Apr 24, 3:47 am, "Willy Denoyette [MVP]"
<willy.denoye...@telenet.bewrote:
"Matt" <matttel...@sprynet.comwrote in message

news:0a**********************************@k37g2000 hsf.googlegroups.com...
On Apr 23, 3:58 pm, "Willy Denoyette [MVP]"

<willy.denoye...@telenet.bewrote:
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac...@gmail.comwrote in
messagenews:3d**********************************@a 22g2000hsc.googlegroups.com...
On Apr 23, 12:47 pm, greatbarrier86
<greatbarrie...@discussions.microsoft.comwrote:
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.

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.
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.

Jun 27 '08 #10

P: n/a
greatbarrier86 wrote:
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

Jun 27 '08 #11

This discussion thread is closed

Replies have been disabled for this discussion.