2

我想将映射网络驱动器的路径添加到 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

4

1 回答 1

0

如果您使用本机 WINAPI 调用来实现您想要的,而不是脚本对象,您将获得更多的控制权。这些解决方案为您提供了更好的控制,并且在http://pinvoke.net等网站的帮助下,您总能找到 ac# 包装器或轻松创建一个。如果在 .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
}
于 2014-06-26T19:58:09.040 回答