Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use CreateFile API function for network path?

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

like image 204
kombsh Avatar asked Sep 08 '26 04:09

kombsh


1 Answers

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().

like image 107
Hans Passant Avatar answered Sep 11 '26 05:09

Hans Passant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!