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(); }
public void RenameFolder() { var setting = this.Settings.First(); var client = new SMB1Client(); var connected = client.Connect(IPAddress.Parse(setting.Address), SMBTransportType.DirectTCPTransport); if (connected) { var loginStatus = client.Login(string.Empty, setting.UserName, setting.Password); if (loginStatus == NTStatus.STATUS_SUCCESS) { var store = client.TreeConnect(setting.TestPath.Share, out var shareStatus) as SMB1FileStore; if (shareStatus == NTStatus.STATUS_SUCCESS) { var createStatus = store.CreateFile( out var handle, out _, @"\\Musics\created", AccessMask.GENERIC_ALL | AccessMask.SYNCHRONIZE, 0, ShareAccess.None, CreateDisposition.FILE_OPEN, CreateOptions.FILE_DIRECTORY_FILE, null ); if (createStatus == NTStatus.STATUS_SUCCESS) { var info = new FileRenameInformationType1() { FileName = @"\\updated", }; var renameStatus = store.SetFileInformation(handle, info); Debug.WriteLine((renameStatus == NTStatus.STATUS_SUCCESS) ? "Succeeded!" : "Failed..."); store.CloseFile(handle); } store.Disconnect(); } } client.Disconnect(); } }
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(); }