public void readSmbFile() { SMB1Client client = new SMB1Client(); // SMB2Client can be used as well bool isConnected = client.Connect(IPAddress.Parse("192.168.10.200"), SMBTransportType.DirectTCPTransport); if (isConnected) { NTStatus status = client.Login(String.Empty, "jing.luo", "ximai_2016"); ISMBFileStore fileStore = client.TreeConnect("software", out status); object fileHandle; FileStatus fileStatus; string filePath = "EXCEL在财务管理中的高级应用.pdf"; if (fileStore is SMB1FileStore) { filePath = @"\\" + filePath; } status = fileStore.CreateFile(out fileHandle, out fileStatus, filePath, AccessMask.GENERIC_READ | AccessMask.SYNCHRONIZE, (SMBLibrary.FileAttributes)System.IO.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.MemoryStream stream = new System.IO.MemoryStream(); byte[] data; long bytesRead = 0; while (true) { status = fileStore.ReadFile(out data, fileHandle, bytesRead, (int)client.MaxReadSize); if (status != NTStatus.STATUS_SUCCESS && status != NTStatus.STATUS_END_OF_FILE) { throw new Exception("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(); } //if (status == NTStatus.STATUS_SUCCESS) //{ // List<string> shares = client.ListShares(out status); // client.Logoff(); //} //client.Disconnect(); }
protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { if (pipeHandle != null && NamedPipeShare != null) { NamedPipeShare.CloseFile(pipeHandle); } if (NamedPipeShare != null) { NamedPipeShare.Disconnect(); } } disposedValue = true; } }
private void tsbExplore_Click(object sender, EventArgs e) { SMB1Client client = new SMB1Client(); // SMB2Client can be used as well bool isConnected = client.Connect(IPAddress.Parse("192.168.10.200"), SMBTransportType.DirectTCPTransport); NTStatus status = client.Login(String.Empty, "jing.luo", "ximai_2016"); if (isConnected) { if (status == NTStatus.STATUS_SUCCESS) { List <string> shares = client.ListShares(out status); foreach (var item in shares) { richTextBox1.Text += item.ToString() + " "; } } } ISMBFileStore fileStore = client.TreeConnect("5S", out status); //if (status == NTStatus.STATUS_SUCCESS) //{ // object directoryHandle; // FileStatus fileStatus; // status = fileStore.CreateFile(out directoryHandle, out fileStatus, // "\\", AccessMask.GENERIC_READ, FileAttributes.Directory, // ShareAccess.Read | ShareAccess.Write, CreateDisposition.FILE_OPEN, // CreateOptions.FILE_DIRECTORY_FILE, null); // if (status == NTStatus.STATUS_SUCCESS) // { // List<FindInformation> fileList2; // status = ((SMB1FileStore)fileStore).QueryDirectory(out fileList2, "\\*", // FindInformationLevel.SMB_FIND_FILE_DIRECTORY_INFO); // //status = fileStore.CloseFile(directoryHandle); // foreach (var item in fileList2) // { // //richTextBox1.Text += item.; // } // } //} status = fileStore.Disconnect(); }
public List <string> ListShares(out NTStatus status) { if (!m_isConnected || !m_isLoggedIn) { throw new InvalidOperationException("A login session must be successfully established before retrieving share list"); } ISMBFileStore namedPipeShare = TreeConnect("IPC$", out status); if (namedPipeShare == null) { return(null); } List <string> shares = ServerServiceHelper.ListShares(namedPipeShare, SMBLibrary.Services.ShareType.DiskDrive, out status); namedPipeShare.Disconnect(); return(shares); }
public void Dispose() { fileStore.Disconnect(); }