在C#中映射网络驱动器路径

时间:2013-04-14 08:48:13

标签: c#

我想将地图网络驱动器的路径添加到C#代码中并在用户点击它时链接它自动打开我的代码就像

的驱动器路径
IWshNetwork_Class network = new IWshNetwork_Class();
network.MapNetworkDrive("Z:", @"\\ms\temp");
string message1 = @"[1] Go to C:\" + Environment.NewLine + "[2] Replace Folder \"myTeX\" with myTeX exist in" + network + Environment.NewLine + "[3] Open Start menu-->All Programs-->MiKTeX 2.8-->Maintenance-->Settings-->Click on \"Refresh FNDB\" button then wait the process and try again, Good Luck ";
richTextBox2.Text = message1;

但由于message1中出现的名称与

相似,此代码无法正常运行
[1] Go to C:\
[2] Replace Folder "myTeX" with myTeX exist inSystem.__ComObject
[3] Open Start menu-->All Programs-->MiKTeX 2.8-->Maintenance-->Settings-->Click on "Refresh FNDB" button then wait the process and try again, Good Luck 

所以System._ComObject应为\\ms\temp,并在用户点击它时链接到驱动器\\ms\temp

1 个答案:

答案 0 :(得分:0)

如果您使用本机WINAPI调用来实现所需的内容,则可以获得更多控制权,而不是脚本对象。这些解决方案可以让您更好地控制,在http://pinvoke.net之类的网站的帮助下,您总能找到一个c#包装器或轻松创建一个。如果.Net Framwork类中的一个解决方案存在,您经常会找到一个链接。

您基本上需要WINAPI中的两个函数:

此解决方案包括映射和获取信息的方法以及一些帮助

映射驱动器和显示信息的方法

var mapResult = WNetAddConnection2(
    new NETRESOURCE {
    Scope = ResourceScope.Connected,
    ResourceType = ResourceType.Disk,
    DisplayType = ResourceDisplaytype.Generic,
    Usage = 0,
    LocalName = @"Z:",
    RemoteName = @"\\ms\temp",
    Comment = "from csharp",
    Provider = null
    }
    , null
    , null
    , 0);
if (mapResult!=0)
{
    throw new Exception("AddConnection failed");
    // >0? check  http://msdn.microsoft.com/en-us/library/windows/desktop/ms681383(v=vs.85).aspx
}

// get the remote connection path
int remoteBufLen = 0; // start with a 0 length buffer
StringBuilder remotePath = null;
var connRes = ERROR_MORE_DATA;  
// call twice if the buffer is too small
for (int t=0 ; t<2 && connRes == ERROR_MORE_DATA; t++)
{
    remotePath = new StringBuilder(remoteBufLen);
    // and error is returned 
    // and remoteBufLen holds the required size
    connRes = WNetGetConnection(@"Z:", remotePath, ref remoteBufLen);
}
if (connRes != 0)
{
   throw new Exception("getconnetion failed");
}

string message1 = String.Format(@"[1] Go to C:\{0}[2] Replace Folder ""myTeX"" with myTeX exist in {1}{0}[3] Open Start menu-->All Programs-->MiKTeX 2.8-->Maintenance-->Settings-->Click on ""Refresh FNDB"" button then wait the process and try again, Good Luck "
, Environment.NewLine, remotePath);

richTextBox2.Text = message1;

助手

我使用http://pinvoke.net查找WINAPI调用的c#声明

// http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx
const int ERROR_MORE_DATA =0xEA;

// http://www.pinvoke.net/default.aspx/mpr.WNetGetConnection
[DllImport("mpr.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern int WNetGetConnection(
    [MarshalAs(UnmanagedType.LPTStr)] string localName, 
    [MarshalAs(UnmanagedType.LPTStr)] StringBuilder remoteName, 
    ref int length);

//http://www.pinvoke.net/default.aspx/mpr.VVNetAddConnection2
[DllImport("mpr.dll")]    
public static extern int WNetAddConnection2(NETRESOURCE netResource,
    string password, string username, uint flags);


[StructLayout(LayoutKind.Sequential)]
public class NETRESOURCE
{
     public ResourceScope Scope;
     public ResourceType ResourceType;
     public ResourceDisplaytype DisplayType;
     public int Usage;
     public string LocalName;
     public string RemoteName;
     public string Comment;
     public string Provider;
}

public enum ResourceScope : int
{
     Connected = 1,
     GlobalNetwork,
     Remembered,
     Recent,
     Context
}

public enum ResourceType : int
{
    Any = 0,
     Disk = 1,
     Print = 2,
     Reserved = 8,
}

public enum ResourceDisplaytype : int
{
     Generic = 0x0,
     Domain = 0x01,
     Server = 0x02,
     Share = 0x03,
     File = 0x04,
     Group = 0x05,
     Network = 0x06,
     Root = 0x07,
     Shareadmin = 0x08,
     Directory = 0x09,
     Tree = 0x0a,
     Ndscontainer = 0x0b
}