473,440 Members | 1,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,440 software developers and data experts.

Mapping Network Drive in C#

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
10 10967
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
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
"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
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
"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
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
"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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thierry Lam | last post by:
On windows, if I want to map a network drive to a local drive on my computer, I do the following: data = { 'remote' : '\\blah\data', 'local' : 'Z:' } win32net.NetUseAdd(None, 1, data)
2
by: giloosh99 | last post by:
Hello, Im grabbing tables via VB code using visual foxpro ODBC drives. The tables directory is in a mapped network drive. The code works fine and does the job, however if the computer is idle for...
1
by: Slawek | last post by:
Hello, In my application (WinForm) I need to read data from files located on other computers. I know passwords and user names requied to map these natwork drives but I don't know how to map it...
5
by: Niloday | last post by:
Hi All, I am trying to access a mapped network drive from a service that I have created. The service needs to create/delete folders/files on a network drive. When I tried to connect to a...
2
by: CrystalBlue | last post by:
I've got a silly little problem when I'm trying to map a drive using shell script. Basically, I need the program to open up to a specific folder on a shared server and throw a zip file onto it, then...
0
by: =?Utf-8?B?Z3JlYXRiYXJyaWVyODY=?= | last post by:
Hi,
20
by: =?Utf-8?B?QnJpYW4gTmljaG9sc29u?= | last post by:
Hello, I'm trying to create an admin page that can run miscellaneous scripts for our IT department. Currently, I'm trying to create a script that can map a network drive for a user (e.g. a form...
5
by: Phil | last post by:
A file used by my application may be stored on a shared network drive, so that it can be accessed by different people using the same application. I have written some code that uses an...
0
by: rupelo78 | last post by:
Morning folks, if you all could loan me a hand it would be sweet. New to VB script, so pardon me for the noob ways. I am trying to useogon script that would map our HP LaserJet 4600s. all these...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
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...

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.