回答
使用本地mpr.dll
中的WnetAddConnection
功能。
您將必須編寫P/Invoke簽名和結構來調用非託管函數。您可以在pinvoke.net上的P/Invoke上找到資源。
這是the signature for WNetAddConnection2
on pinvoke.net:
[DllImport("mpr.dll")]
public static extern int WNetAddConnection2(
ref NETRESOURCE netResource,
string password,
string username,
int flags);
看一看@NetShareAdd Windows的API。當然,您需要使用PInvoke來實現它。
有一個在.NET中沒有標準的功能映射networkdrives,但你可以在這裏找到一個很好的包裝,如果你不想自己來執行本地電話:http://www.codeguru.com/csharp/csharp/cs_network/windowsservices/article.php/c12357
更直接的解決方案是使用Process.Start()
internal static int RunProcess(string fileName, string args, string workingDir)
{
var startInfo = new ProcessStartInfo
{
FileName = fileName,
Arguments = args,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
CreateNoWindow = true,
WorkingDirectory = workingDir
};
using (var process = Process.Start(startInfo))
{
if (process == null)
{
throw new Exception($"Failed to start {startInfo.FileName}");
}
process.OutputDataReceived += (s, e) => e.Data.Log();
process.ErrorDataReceived += (s, e) =>
{
if (!string.IsNullOrWhiteSpace(e.Data)) { new Exception(e.Data).Log(); }
};
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
return process.ExitCode;
}
}
一旦你有以上,使用下面創建/刪除根據需要映射驅動器。
Converter.RunProcess("net.exe", @"use Q: \\server\share", null);
Converter.RunProcess("net.exe", "use Q: /delete", null);
簡化版本 - 確保您使用System.Diagnostics;爲的System.Diagnostics.Process: \t'炭驅動器號= 'R';' \t'串路徑= @ 「\\ CONTOSO \共享\物」;' \t'的Process.Start(新的ProcessStartInfo(@「C: \ Windows \ System32 \ net.exe「,」使用「+ DriveLetter +」:「+ Path));' – omJohn8372 2017-04-05 19:02:35
- 1. 如何使用PowerShell映射驅動器
- 2. Cassandra C#驅動程序映射器
- 3. Gradle - 如何映射驅動器?
- 4. 映射網絡驅動器
- 5. 映射網絡驅動器
- 6. 映射Sharepoint驅動器
- 7. Delphi - 映射驅動器ipaddress
- 8. 如何使用我剛剛在網絡使用中映射的驅動器? (C++)
- 9. 如何在C#中使用net use將samba服務器驅動器映射到某些本地驅動器?
- 10. 如何使用asp.net上傳映射驅動器中的文件?
- 11. 如何使用asp.net上傳映射驅動器中的文件?
- 12. 如何在C#中獲得網絡映射驅動器VolumeLabel?
- 13. 如何使用C#將ftp共享文件夾映射到本地驅動器?
- 14. 映射使用自動映射器
- 15. PowerShell新PSD驅動器沒有映射驅動器
- 16. 如何使用Datastax C#驅動程序將C#枚舉映射到Cassandra字段?
- 17. 我如何映射網絡驅動器與用戶憑證
- 18. 如何獲得可用的網絡驅動器映射?
- 19. Gem install尋找映射驅動器
- 20. 根據組成員映射驅動器
- 21. Caspol,虛擬機,映射驅動器,VS2010
- 22. Python Win32 - DriveInfo在映射驅動器
- 23. 將驅動器映射到SharePoint 2010
- 24. Cygwin訪問Windows UNC映射驅動器
- 25. SMB和Python與映射驅動器
- 26. VMWare VMDK映射到Windows驅動器號
- 27. Powershell:如何使用不同的用戶名/密碼映射網絡驅動器
- 28. 映射網絡驅動器無法訪問使用WMI
- 29. 使用模擬時訪問映射驅動器
- 30. 使用PowerShell安裝Azure文件服務映射驅動器
'NetShareAdd'創建共享。問題是關於如何映射現有份額。 – 2010-08-12 09:19:57