interop CreateFileW : Access Denied

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(
                        path,
                        NativeMethods.EFileAccess.GenericWrite,
                        NativeMethods.EFileShare.None,
                        IntPtr.Zero,
                        NativeMethods.ECreationDisposition.OpenAlways,
                        NativeMethods.EFileAttributes.Normal,
                        IntPtr.Zero);
                  if (handle == NativeMethods.INVALID_HANDLE_VALUE)
                  {
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                  }
                  else
                  {
                        return new FileStream(handle, FileAccess.ReadWrite, true);
                  }
            }
      }

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

            [Flags]
                  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,
            }

            [Flags]
                  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);
      }
LVL 6
wimthepimscakeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mikal613Commented:
did you add this dll in another project and its still open?
0
wimthepimscakeAuthor Commented:
aha, I did, how do I close it?
0
Mikal613Commented:
just close the project that you added it to
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

wimthepimscakeAuthor Commented:
It is in the same project but in another dll
0
Mikal613Commented:
then recompile the whole project with the Dll included
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
c_myersCommented:
FYI, not sure if you saw this already, but it may provide some useful hints in case you run into other problems:

http://pinvoke.net/default.aspx/kernel32.CreateFile
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.