Reputation: 5506
I have come across the CreateFile API for C drive. It is working fine. But when I try to use a network share path it throws an error.
private void GetRootHandle()
{
string vol = string.Concat(@"\\192.168.1.24\share1");
_changeJournalRootHandle = PInvokeWin32.CreateFile(vol,
PInvokeWin32.GENERIC_READ | PInvokeWin32.GENERIC_WRITE,
PInvokeWin32.FILE_SHARE_READ | PInvokeWin32.FILE_SHARE_WRITE,
IntPtr.Zero,
PInvokeWin32.OPEN_EXISTING,
0,
IntPtr.Zero);
if (_changeJournalRootHandle.ToInt32() == PInvokeWin32.INVALID_HANDLE_VALUE)
{
throw new IOException("CreateFile() returned invalid handle",
new Win32Exception(Marshal.GetLastWin32Error()));
}
}
Can any one give me an idea how to use share path for this API function
Upvotes: 3
Views: 3123
Reputation: 7271
The access denied error is caused because you do not have permissions to open the file or folder with the privileges you have requested. In this case, read and write. GetFileInformationByHandle does not require write privileges. You should remove PInvokeWin32.GENERIC_WRITE
from the desiredAccess
(2nd) parameter.
The MSDN docs for CreateFile say you can pass in 0 to the desiredAccess
parameter and use the handle in GetFileInformationByHandle
.
The dwDesiredAccess parameter can be zero, allowing the application to query file attributes without accessing the file if the application is running with adequate security settings. This is useful to test for the existence of a file without opening it for read and/or write access, or to obtain other statistics about the file or directory. See Obtaining and Setting File Information and GetFileInformationByHandle.
However, you will probably be better served by the Directory class in C# for retrieving directory information.
Upvotes: 1
Reputation: 942408
I am getting Access is denied exception
That's because it is not a file. Get ahead by not using CreateFile(), it just isn't necessary. Use the regular .NET classes, like DirectoryInfo.EnumerateFiles() to enumerate files, FileStream to open them. Use the FileStream.SafeFileHandle property if you need to pinvoke GetFileInformationByHandle().
Upvotes: 2