【问题标题】:problems using sharpcifs with dotnet core. Connecting to a windows share使用带有 dotnet 核心的Sharpcifs 的问题。连接到 Windows 共享
【发布时间】:2020-06-16 18:16:47
【问题描述】:

我尝试从其他 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 共享的方法?

【问题讨论】:

  • 您找到解决方案了吗?
  • O 是的,我最终使用 net.exe 创建网络驱动器。
  • 很公平。遇到这个问题后,我也放弃了 SharpCifs。感谢您发布您的答案!

标签: .net-core smb


【解决方案1】:

我结束了使用 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();
        }
    ```

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    • 1970-01-01
    • 2019-09-25
    • 2021-03-09
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    相关资源
    最近更新 更多