You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SMB1Clientclient=newSMB1Client();// SMB2Client can be used as wellboolisConnected=client.Connect(IPAddress.Parse("192.168.1.11"),SMBTransportType.DirectTCPTransport);if(isConnected){NTStatusstatus=client.Login(String.Empty,"Username","Password");if(status==NTStatus.STATUS_SUCCESS){List<string>shares=client.ListShares(outstatus);client.Logoff();}client.Disconnect();}
Connect to share and list files and directories - SMB1:
ISMBFileStorefileStore=client.TreeConnect("Shared",outstatus);objectfileHandle;FileStatusfileStatus;stringfilePath="IMG_20190109_174446.jpg";if(fileStoreisSMB1FileStore){filePath=@"\\"+filePath;}status=fileStore.CreateFile(outfileHandle,outfileStatus,filePath,AccessMask.GENERIC_READ|AccessMask.SYNCHRONIZE,FileAttributes.Normal,ShareAccess.Read,CreateDisposition.FILE_OPEN,CreateOptions.FILE_NON_DIRECTORY_FILE|CreateOptions.FILE_SYNCHRONOUS_IO_ALERT,null);if(status==NTStatus.STATUS_SUCCESS){System.IO.MemoryStreamstream=newSystem.IO.MemoryStream();byte[]data;longbytesRead=0;while(true){status=fileStore.ReadFile(outdata,fileHandle,bytesRead,(int)client.MaxReadSize);if(status!=NTStatus.STATUS_SUCCESS&&status!=NTStatus.STATUS_END_OF_FILE){thrownewException("Failed to read from file");}if(status==NTStatus.STATUS_END_OF_FILE||data.Length==0){break;}bytesRead+=data.Length;stream.Write(data,0,data.Length);}}status=fileStore.CloseFile(fileHandle);status=fileStore.Disconnect();
Create a file and write to it:
ISMBFileStorefileStore=client.TreeConnect("Shared",outstatus);stringfilePath="NewFile.txt";if(fileStoreisSMB1FileStore){filePath=@"\\"+filePath;}objectfileHandle;FileStatusfileStatus;status=fileStore.CreateFile(outfileHandle,outfileStatus,filePath,AccessMask.GENERIC_WRITE|AccessMask.SYNCHRONIZE,FileAttributes.Normal,ShareAccess.None,CreateDisposition.FILE_CREATE,CreateOptions.FILE_NON_DIRECTORY_FILE|CreateOptions.FILE_SYNCHRONOUS_IO_ALERT,null);if(status==NTStatus.STATUS_SUCCESS){intnumberOfBytesWritten;byte[]data=System.Text.ASCIIEncoding.ASCII.GetBytes("Hello");status=fileStore.WriteFile(outnumberOfBytesWritten,fileHandle,0,data);if(status!=NTStatus.STATUS_SUCCESS){thrownewException("Failed to write to file");}status=fileStore.CloseFile(fileHandle);}status=fileStore.Disconnect();
Write a large file:
ISMBFileStorefileStore=client.TreeConnect("Shared",outstatus);if(status!=NTStatus.STATUS_SUCCESS){thrownewException("Failed to connect to share");}stringlocalFilePath=@"C:\Image.jpg";stringremoteFilePath="NewFile.jpg";if(fileStoreisSMB1FileStore){remoteFilePath=@"\\"+remoteFilePath;}FileStreamlocalFileStream=newFileStream(localFilePath,FileMode.Open,FileAccess.Read);objectfileHandle;FileStatusfileStatus;status=fileStore.CreateFile(outfileHandle,outfileStatus,remoteFilePath,AccessMask.GENERIC_WRITE|AccessMask.SYNCHRONIZE,FileAttributes.Normal,ShareAccess.None,CreateDisposition.FILE_CREATE,CreateOptions.FILE_NON_DIRECTORY_FILE|CreateOptions.FILE_SYNCHRONOUS_IO_ALERT,null);if(status==NTStatus.STATUS_SUCCESS){intwriteOffset=0;while(localFileStream.Position<localFileStream.Length){byte[]buffer=newbyte[(int)client.MaxWriteSize];intbytesRead=localFileStream.Read(buffer,0,buffer.Length);if(bytesRead<(int)client.MaxWriteSize){Array.Resize<byte>(refbuffer,bytesRead);}intnumberOfBytesWritten;status=fileStore.WriteFile(outnumberOfBytesWritten,fileHandle,writeOffset,buffer);if(status!=NTStatus.STATUS_SUCCESS){thrownewException("Failed to write to file");}writeOffset+=bytesRead;}status=fileStore.CloseFile(fileHandle);}status=fileStore.Disconnect();