Bri*_*ndy 14
执行此操作的最佳方法是p/invoke WNetUseConnection.
[StructLayout(LayoutKind.Sequential)]
private class NETRESOURCE
{
public int dwScope = 0;
public int dwType = 0;
public int dwDisplayType = 0;
public int dwUsage = 0;
public string lpLocalName = "";
public string lpRemoteName = "";
public string lpComment = "";
public string lpProvider = "";
}
[DllImport("Mpr.dll")]
private static extern int WNetUseConnection(
IntPtr hwndOwner,
NETRESOURCE lpNetResource,
string lpPassword,
string lpUserID,
int dwFlags,
string lpAccessName,
string lpBufferSize,
string lpResult
);
Run Code Online (Sandbox Code Playgroud)
zen*_*ndu 11
来自http://social.msdn.microsoft.com/Forums/vstudio/en-US/287ca606-86da-4794-baed-2ad5db9bc833/access-to-remote-folder的非常优雅的解决方案.这个只使用.Net库,不需要使用任何命令行或Win32 API.
准备参考的代码:
NetworkCredential theNetworkCredential = new NetworkCredential(@"domain\username", "password");
CredentialCache theNetCache = new CredentialCache();
theNetCache.Add(new Uri(@"\\computer"), "Basic", theNetworkCredential);
string[] theFolders = Directory.GetDirectories(@"\\computer\share");
Run Code Online (Sandbox Code Playgroud)
您可以使用WindowsIdentity类(使用登录标记)在读取和写入文件时进行模拟.
var windowsIdentity = new WindowsIdentity(logonToken);
using (var impersonationContext = windowsIdentity.Impersonate()) {
// Connect, read, write
}
Run Code Online (Sandbox Code Playgroud)
Pre*_*gha -1
您可以使用 system.diagnostocs.process 调用“net use .... with userid and password”或调用这些命令的命令 shell。
| 归档时间: |
|
| 查看次数: |
100654 次 |
| 最近记录: |