5

I try to connect to a windows share from a other windows machine. after a long search i found sharpcifsstd.dobes.jp but i cant get the examples to work.

I receive a SharpCifs.Smb.SmbException: 'Failed to connect, IOException: transport closed in negotiate

I tryed several examples like.

            //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();

I can connect to the windows share with windows explorer.

Has anyone got this to work? or a other method to connect to a windows share?

Bram
  • 61
  • 2

1 Answers1

0

I ended using C:\Windows\System32\net.exe with the use command to create a network drive that can be accessed like any other drive on 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();
        }
    ```
Bram
  • 61
  • 2