Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
if (hasPublic)
            {
            //map drive
            //I am not sure of the lines to write so that I can map a specific
            //Network drive once the button has been clicked
Posted
Updated 23-Jun-14 4:23am
v2
Comments
Pheonyx 23-Jun-14 10:40am    
What have you tried? What have you looked up?
Member 10866610 23-Jun-14 10:46am    
I have tried following the instructions on how to map a network drive via this guide:
https://workspaces.codeproject.com/user-10832455/map-network-drive-api
However I was not able to create a command that works.

1 solution

Why not just call a command line with appropiate net use command - so Process.Start...

So here is an runnable example - just replace Server and Share with real network share:

C#
using System.Diagnostics;

namespace MapNetworkDrive
{
    class Program
    {
        static void Main(string[] args)
        {
            string strCommand = @"net use X: \\Server\Share";

            Process.Start("CMD.exe", "/C \"" + strCommand + "\"");
        }
    }
}


So the idea is to call the commandprompt (cmd.exe) with the net use command to create the mapping. So Process.Start is used to run the commandprompt.

P.S. If you don't want to see the cmd-Window use
C#
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;


I hope this solves your Problem!
 
Share this answer
 
v2
Comments
Member 10866610 23-Jun-14 10:56am    
@ Johannesnestler
Sorry, I am unaware of how to use Process.start as when i attempted I just kept getting errors as it just comes out as black text with red highlights.

I have tried:
using.System.Diagnostics;
process.start("// in here I am unsure of what to enter so that I can map a specified network drive")
johannesnestler 24-Jun-14 12:41pm    
Funny, I wasn't informed about your comment - anyway I updated the solution with an runnable example. - give it a try (and upvote and accept if it fit's your needs please)
Yeah, the notification mechanism is not currently working perfectly. Bug is reported. Will be fully functional soon.

Thanks,
Tadit
Member 10866610 25-Jun-14 6:20am    
@ Johannesnestler, Thanks, that works perfectly,
If I wanted to through in extra security so that they get prompted for a username or password how would that work?

Currently, I have it set so that once a button is clicked the the CMD begins to map the drive but I want it to prompt for a domain username and password first
johannesnestler 25-Jun-14 7:02am    
Just let your user enter Username/Password in a GUI (Dialog whatever) in your Forms app, then call the "net use" command with the additional (username, Password) parameters (you have to build the mapping string dynamically). - for reference how to call "net use" with username/password this was the first hit on Google for me: http://pcsupport.about.com/od/commandlinereference/p/net-use-command.htm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900