gpt4 book ai didi

.net-core - 使用带有 dotnet 核心的Sharpcifs 的问题。连接到 Windows 共享

转载 作者:行者123 更新时间:2023-12-03 17:26:19 25 4
gpt4 key购买 nike

我尝试从其他 Windows 机器连接到 Windows 共享。
经过长时间的搜索,我找到了sharpcifsstd.dobes.jp,但我无法让这些示例发挥作用。

我收到了 SharpCifs.Smb.SmbException: 'Failed to connect, IOException: transport closed in negotiate
我尝试了几个例子。

            //Get the SmbFile specifying the file name to be created.
var auth1 = new NtlmPasswordAuthentication("username","password");
var file = new SmbFile("smb://10.50.15.91/d/NewFileName.txt",auth1);

//Create file.
file.CreateNewFile();

//Get writable stream.
var writeStream = file.GetOutputStream();

//Write bytes.
writeStream.Write(Encoding.UTF8.GetBytes("Hello!"));

//Dispose writable stream.
writeStream.Dispose();

我可以使用 Windows 资源管理器连接到 Windows 共享。

有没有人让这个工作?或连接到 Windows 共享的其他方法?

最佳答案

我结束使用 C:\Windows\System32\net.exe 和 use 命令来创建一个可以像 Windows 上的任何其他驱动器一样访问的网络驱动器。

        public void CopyStuff()
{

var networkDrive = GetLocalFreeDrive();
try
{

var cmd = "use " + networkDrive + " \\\\"+ clientHost + "\\" + drive + " " + clientPassword + " / User:" + clientUsername;
var process = System.Diagnostics.Process.Start("C:\\Windows\\System32\\net.exe", cmd);
process.WaitForExit();



//Do copy stuff from networkDrive


}
catch (Exception e)
{
var process2 = System.Diagnostics.Process.Start("C:\\Windows\\System32\\net.exe", "use /DELETE " + networkDrive);
process2.WaitForExit();
throw (e);
}
var process3 = System.Diagnostics.Process.Start("C:\\Windows\\System32\\net.exe", "use /DELETE " + networkDrive);
process3.WaitForExit();
}

public string GetLocalFreeDrive()
{
DriveInfo[] allDrives = DriveInfo.GetDrives();
ArrayList freeDrives = new ArrayList { "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

foreach (DriveInfo d in allDrives)
{
var drive = d.Name.Substring(0, 1);
if (freeDrives.Contains(drive))
{
freeDrives.Remove(drive);
}
}
return freeDrives[0].ToString();
}
```

关于.net-core - 使用带有 dotnet 核心的Sharpcifs 的问题。连接到 Windows 共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60522941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com