Comments (8)
Hi Hagay,
Basic client functions are supported and should work.
Other than checking for status at the end of each operation, your code seems correct,
First you connect to the server, then you connect to the share using TreeConnect, once you connect to the share, you use CreateFile to get a file handle (as you would with the Windows Native API).
I have tested your code against Windows 7 SP1 as server, and I'm successfully creating the file,
Please note that STATUS_ACCESS_DENIED is coming from your server, not the client, which simply report back the status returned from the server (you can use WireShark to verify this).
Are you sure that the user you use for login have both share level permissions and file level permissions that allow for creating the file? Which Windows version do you use for server?
from smblibrary.
Hi Tal,
Thanks for your quick reply.
The server is latest windows 10 pro.
I Don't have at the moment a windows 7 machine to test against.
Maybe i am missing out on the path convention ?
On windows explorer the file is available on :
\\192.168.120.10\shared\zzz.txxt
and of course my username is able to list the content of :
\\192.168.120.10\shared
"shared" is of course available on the returned shares variable from ListShares method.
Can you share a working code to list the content of a share path (similar to my example) ?
Thanks Again,
Hagay
from smblibrary.
Here is a Working example that I tested against Windows 7 SP1 / 8.1 / 10:
SMB2Client client = new SMB2Client();
bool success = client.Connect(System.Net.IPAddress.Parse("192.168.1.121"), SMBTransportType.DirectTCPTransport);
if (success)
{
NTStatus status = client.Login(String.Empty, username, password);
if (status == NTStatus.STATUS_SUCCESS)
{
SMB2FileStore fileStore = client.TreeConnect("Shared", out status) as SMB2FileStore;
if (fileStore != null)
{
object handle;
FileStatus fileStatus;
// Open existing file for reading
status = fileStore.CreateFile(out handle, out fileStatus, "Notes.txt", AccessMask.GENERIC_READ, 0, ShareAccess.Read, CreateDisposition.FILE_OPEN, CreateOptions.FILE_NON_DIRECTORY_FILE, null);
if (status == NTStatus.STATUS_SUCCESS)
{
byte[] data;
status = fileStore.ReadFile(out data, handle, 0, 4096);
fileStore.CloseFile(handle);
if (status != NTStatus.STATUS_SUCCESS)
{
return;
}
// Write the data to another file (create it if it does not exist)
status = fileStore.CreateFile(out handle, out fileStatus, "Copy of Notes.txt", AccessMask.GENERIC_READ | AccessMask.GENERIC_WRITE, 0, ShareAccess.None, CreateDisposition.FILE_OPEN_IF, CreateOptions.FILE_NON_DIRECTORY_FILE, null);
if (status == NTStatus.STATUS_SUCCESS)
{
int numberOfBytesWritten;
status = fileStore.WriteFile(out numberOfBytesWritten, handle, 0, data);
fileStore.CloseFile(handle);
}
}
}
}
}
from smblibrary.
Thank you very much , i managed to get the file creation/copy to work , also list non "share root" folder :
var smb = new SMB2Client();
var b = smb.Connect(new System.Net.IPAddress(new byte[] { 192, 168, 120, 10 }), SMBLibrary.SMBTransportType.DirectTCPTransport);
var status = smb.Login(string.Empty, Username, password);
SMBLibrary.NTStatus actionStatus;
var shares = smb.ListShares(out actionStatus);
var tree = smb.TreeConnect("shared", out status) as SMB2FileStore;
object handle;
SMBLibrary.FileStatus fs;
// list \\192.168.20.10\shared - share root - returns STATUS_INVALID_PARAMETER
actionStatus = tree.CreateFile(out handle, out fs, "", AccessMask.GENERIC_READ, 0, ShareAccess.Read | ShareAccess.Write | ShareAccess.Delete, CreateDisposition.FILE_OPEN, CreateOptions.FILE_DIRECTORY_FILE, null);
if (actionStatus == NTStatus.STATUS_SUCCESS)
{
actionStatus = tree.QueryDirectory(out List<QueryDirectoryFileInformation> files, handle, "*", FileInformationClass.FileDirectoryInformation);
}
// list \\192.168.20.10\shared\Backups contents - works
actionStatus = tree.CreateFile(out handle, out fs, "Backups", AccessMask.GENERIC_READ, 0, ShareAccess.Read, CreateDisposition.FILE_OPEN, CreateOptions.FILE_DIRECTORY_FILE, null);
if (actionStatus == NTStatus.STATUS_SUCCESS)
{
actionStatus = tree.QueryDirectory(out List<QueryDirectoryFileInformation> files, handle, "*", FileInformationClass.FileDirectoryInformation);
}
// file read , write works
actionStatus = tree.CreateFile(out handle, out fs, "zzz.txt", AccessMask.GENERIC_READ, 0, ShareAccess.Read, CreateDisposition.FILE_OPEN, CreateOptions.FILE_NON_DIRECTORY_FILE, null);
if (actionStatus == NTStatus.STATUS_SUCCESS)
{
byte[] data;
status = tree.ReadFile(out data, handle, 0, 4096);
tree.CloseFile(handle);
if (status != NTStatus.STATUS_SUCCESS)
{
return;
}
// Write the data to another file (create it if it does not exist)
status = tree.CreateFile(out handle, out fs, "yyy.txt", AccessMask.GENERIC_READ | AccessMask.GENERIC_WRITE, 0, ShareAccess.None, CreateDisposition.FILE_OPEN_IF, CreateOptions.FILE_NON_DIRECTORY_FILE, null);
if (status == NTStatus.STATUS_SUCCESS)
{
int numberOfBytesWritten;
status = tree.WriteFile(out numberOfBytesWritten, handle, 0, data);
tree.CloseFile(handle);
}
}
As for listing share root content, documentation for SMB is very hard to find , i guess "" is correct , becuase if i i use "." i get different status code.
So my guess a different parameters are required than a regular folder query.
from smblibrary.
-
Yes, as you can see in the SMB2 specifications: "A zero length file name indicates a request to open the root of the share".
-
I noticed that there is a specific Windows 8.1 / Windows 10 issue that I now took care of, please update to the latest commit.
-
I have updated the previous code sample (forgot to check if fileStore is not null)
To open the root directory of the share and list it's content:
status = fileStore.CreateFile(out handle, out fileStatus, "", AccessMask.SYNCHRONIZE | (AccessMask)DirectoryAccessMask.FILE_LIST_DIRECTORY, 0, ShareAccess.Read | ShareAccess.Write | ShareAccess.Delete, CreateDisposition.FILE_OPEN, CreateOptions.FILE_SYNCHRONOUS_IO_NONALERT | CreateOptions.FILE_DIRECTORY_FILE, null);
if (status == NTStatus.STATUS_SUCCESS)
{
List<QueryDirectoryFileInformation> result;
status = fileStore.QueryDirectory(out result, handle, "*", FileInformationClass.FileNamesInformation);
}
BTW, You can use WireShark to see which parameters Windows clients use, and you can also check the Native API documentation (not to be confused with the Win32 API)
from smblibrary.
Running my code with your latest commit works fine now.
Thanks a lot for all your help and work.
from smblibrary.
Hi @TalAloni
When trying to use
`
status = fileStore.CreateFile(out object directoryHandle, out FileStatus fileStatus, filePath, 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.FileNamesInformation);
status = fileStore.CloseFile(directoryHandle);
}
`
Gives Not Implemented exception, I wanted to list file names so I can read each file in a loop
from smblibrary.
@rdang93 Please see the existing examples to retrieve list of file names in a directory.
from smblibrary.
Related Issues (20)
- High RAM consumption. HOT 10
- Is it possible to get a login and NTLM? HOT 4
- Making ClientMaxReadSize and ClientMaxWriteSize Configurable HOT 1
- The new Connect method in the 1.5.0.2 version HOT 1
- ACCESS_DENIED_ACE type is missing HOT 1
- Login without user and password HOT 1
- Bug after latest ArrayPool implementation HOT 4
- Issue not receiving ReadResponse when reading a file HOT 16
- Renaming File to one Character HOT 1
- Async exception on sync operation HOT 6
- SMBServer.exe HOT 1
- NullReferenceExceptions in OnClientSocketReceive and unhandeled SocketExceptions in async process HOT 15
- how can I use SMB2 upload larger file in 'ResumeFromBreakPoint' mode? HOT 4
- Copying a file while having an STATUS_SHARING_VIOLATION HOT 1
- UnobservedTaskException after Client.Disconnect() HOT 5
- SMBServer.exe HOT 1
- IsLoggedIn Getter HOT 1
- Exception thrown by .net Framework. HOT 7
- How to Access a File With in the Sub Folders Using SMB2Client
- [Question] FileSystemWatcher-like support HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from smblibrary.