interop CreateFileW : Access Denied

Posted on 2006-04-03
Last Modified: 2011-04-14
Can someone please help me with this, I try to use the following interop code, but I always get an error : Access is denied?

this is the code:

      public class LongPathIO
            public static FileStream OpenLongPath(string path)
                  IntPtr handle = NativeMethods.CreateFileW(
                  if (handle == NativeMethods.INVALID_HANDLE_VALUE)
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                        return new FileStream(handle, FileAccess.ReadWrite, true);

      internal class NativeMethods
            public static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
                  public enum EFileAccess : uint
                  GenericRead = 0x80000000,
                  GenericWrite = 0x40000000,
                  GenericExecute = 0x20000000,
                  GenericAll = 0x10000000,

                  public enum EFileShare : uint
                  None = 0x00000000,
                  Read = 0x00000001,
                  Write = 0x00000002,
                  Delete = 0x00000004,

            public enum ECreationDisposition : uint
                  New = 1,
                  CreateAlways = 2,
                  OpenExisting = 3,
                  OpenAlways = 4,
                  TruncateExisting = 5,

                  public enum EFileAttributes : uint
                  Readonly = 0x00000001,
                  Hidden = 0x00000002,
                  System = 0x00000004,
                  Directory = 0x00000010,
                  Archive = 0x00000020,
                  Device = 0x00000040,
                  Normal = 0x00000080,
                  Temporary = 0x00000100,
                  SparseFile = 0x00000200,
                  ReparsePoint = 0x00000400,
                  Compressed = 0x00000800,
                  Offline= 0x00001000,
                  NotContentIndexed = 0x00002000,
                  Encrypted = 0x00004000,
                  Write_Through = 0x80000000,
                  Overlapped = 0x40000000,
                  NoBuffering = 0x20000000,
                  RandomAccess = 0x10000000,
                  SequentialScan = 0x08000000,
                  DeleteOnClose = 0x04000000,
                  BackupSemantics = 0x02000000,
                  PosixSemantics = 0x01000000,
                  OpenReparsePoint = 0x00200000,
                  OpenNoRecall = 0x00100000,
                  FirstPipeInstance = 0x00080000

            [DllImport("kernel32.dll", EntryPoint="CreateFileW", SetLastError=true, CharSet=CharSet.Unicode, ExactSpelling=true)]
            public static extern IntPtr CreateFileW(
                  string lpFileName,
                  EFileAccess dwDesiredAccess,
                  EFileShare dwShareMode,
                  IntPtr lpSecurityAttributes,
                  ECreationDisposition dwCreationDisposition,
                  EFileAttributes dwFlagsAndAttributes,
                  IntPtr hTemplateFile);
Question by:wimthepimscake
    LVL 48

    Expert Comment

    did you add this dll in another project and its still open?
    LVL 6

    Author Comment

    aha, I did, how do I close it?
    LVL 48

    Expert Comment

    just close the project that you added it to
    LVL 6

    Author Comment

    It is in the same project but in another dll
    LVL 48

    Accepted Solution

    then recompile the whole project with the Dll included
    LVL 4

    Expert Comment

    FYI, not sure if you saw this already, but it may provide some useful hints in case you run into other problems:

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now