嗨,我正在使用Visual Studio 2012终极版的Windows窗体应用程序,如果单击按钮,如何映射特定的网络驱动器?

问题描述:

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

为什么不使用适当的 net use 命令 - 所以 Process.Start ...



所以这里是一个可运行的示例 - 只需用实际网络共享替换服务器和共享:



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:

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 + "\"");
        }
    }
}





所以我的想法就是调用commandprompt(cmd.exe) )使用net use命令创建映射。所以Process.Start用于运行命令提示。



P.S.如果您不想看到cmd-Window,请使用



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

startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;





我希望这能解决你的问题!



I hope this solves your Problem!