473,322 Members | 1,352 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,322 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 10950
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.