mirror of
https://github.com/TalAloni/SMBLibrary.git
synced 2025-08-11 09:59:19 +02:00
Added ClientCodeExamples
This commit is contained in:
parent
0f43ef1f88
commit
fa054927fa
2 changed files with 117 additions and 0 deletions
115
ClientExamples.md
Normal file
115
ClientExamples.md
Normal file
|
@ -0,0 +1,115 @@
|
||||||
|
Login and List Shares:
|
||||||
|
======================
|
||||||
|
```
|
||||||
|
SMB1Client client = new SMB1Client(); // SMB2Client can be used as well
|
||||||
|
bool isConnected = client.Connect(IPAddress.Parse("192.168.1.11"), SMBTransportType.DirectTCPTransport);
|
||||||
|
if (isConnected)
|
||||||
|
{
|
||||||
|
NTStatus status = client.Login(String.Empty, "Username", "Password");
|
||||||
|
if (status == NTStatus.STATUS_SUCCESS)
|
||||||
|
{
|
||||||
|
List<string> shares = client.ListShares(out status);
|
||||||
|
client.Logoff();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Connect to share and list files and directories - SMB1:
|
||||||
|
=======================================================
|
||||||
|
```
|
||||||
|
SMB1FileStore fileStore = client.TreeConnect("Shared", 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
status = fileStore.Disconnect();
|
||||||
|
```
|
||||||
|
|
||||||
|
Connect to share and list files and directories - SMB2:
|
||||||
|
=======================================================
|
||||||
|
```
|
||||||
|
SMB1FileStore fileStore = client.TreeConnect("Shared", out status);
|
||||||
|
if (status == NTStatus.STATUS_SUCCESS)
|
||||||
|
{
|
||||||
|
object directoryHandle;
|
||||||
|
FileStatus fileStatus;
|
||||||
|
status = fileStore.CreateFile(out directoryHandle, out fileStatus, String.Empty, AccessMask.GENERIC_READ, FileAttributes.Directory, ShareAccess.Read | ShareAccess.Write, CreateDisposition.FILE_OPEN, CreateOptions.FILE_DIRECTORY_FILE, null);
|
||||||
|
if (status == NTStatus.STATUS_SUCCESS)
|
||||||
|
{
|
||||||
|
List<QueryDirectoryFileInformation> fileList;
|
||||||
|
status = fileStore.QueryDirectory(out fileList, directoryHandle, "*", FileInformationClass.FileDirectoryInformation);
|
||||||
|
status = fileStore.CloseFile(directoryHandle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
status = fileStore.Disconnect();
|
||||||
|
```
|
||||||
|
|
||||||
|
Read large file to its end:
|
||||||
|
===========================
|
||||||
|
```
|
||||||
|
ISMBFileStore fileStore = client.TreeConnect("Shared", out status);
|
||||||
|
object fileHandle;
|
||||||
|
FileStatus fileStatus;
|
||||||
|
string filePath = "IMG_20190109_174446.jpg";
|
||||||
|
if (fileStore is SMB1FileStore)
|
||||||
|
{
|
||||||
|
filePath = @"\\" + filePath;
|
||||||
|
}
|
||||||
|
status = fileStore.CreateFile(out fileHandle, out fileStatus, 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.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();
|
||||||
|
```
|
||||||
|
|
||||||
|
Delete File:
|
||||||
|
============
|
||||||
|
```
|
||||||
|
ISMBFileStore fileStore = client.TreeConnect("Shared", out status);
|
||||||
|
string filePath = "DeleteMe.txt";
|
||||||
|
if (fileStore is SMB1FileStore)
|
||||||
|
{
|
||||||
|
filePath = @"\\" + filePath;
|
||||||
|
}
|
||||||
|
object fileHandle;
|
||||||
|
FileStatus fileStatus;
|
||||||
|
status = fileStore.CreateFile(out fileHandle, out fileStatus, filePath, AccessMask.GENERIC_WRITE | AccessMask.DELETE | AccessMask.SYNCHRONIZE, FileAttributes.Normal, ShareAccess.None, CreateDisposition.FILE_OPEN, CreateOptions.FILE_NON_DIRECTORY_FILE | CreateOptions.FILE_SYNCHRONOUS_IO_ALERT, null);
|
||||||
|
|
||||||
|
if (status == NTStatus.STATUS_SUCCESS)
|
||||||
|
{
|
||||||
|
FileDispositionInformation fileDispositionInformation = new FileDispositionInformation();
|
||||||
|
fileDispositionInformation.DeletePending = true;
|
||||||
|
status = fileStore.SetFileInformation(fileHandle, fileDispositionInformation);
|
||||||
|
bool deleteSucceeded = (status == NTStatus.STATUS_SUCCESS);
|
||||||
|
status = fileStore.CloseFile(fileHandle);
|
||||||
|
}
|
||||||
|
status = fileStore.Disconnect();
|
||||||
|
```
|
|
@ -55,6 +55,8 @@ Using SMBLibrary:
|
||||||
Any directory / filesystem / object you wish to share must implement the IFileSystem interface (or the lower-level INTFileStore interface).
|
Any directory / filesystem / object you wish to share must implement the IFileSystem interface (or the lower-level INTFileStore interface).
|
||||||
You can share anything from actual directories to custom objects, as long as they expose a directory structure.
|
You can share anything from actual directories to custom objects, as long as they expose a directory structure.
|
||||||
|
|
||||||
|
Client code examples can be found [here](ClientExamples.md).
|
||||||
|
|
||||||
Contact:
|
Contact:
|
||||||
========
|
========
|
||||||
If you have any question, feel free to contact me.
|
If you have any question, feel free to contact me.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue