We help IT Professionals succeed at work.

interop CreateFileW : Access Denied

wimthepimscake
on
Medium Priority
894 Views
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(
                        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);
      }
Comment
Watch Question

CERTIFIED EXPERT

Commented:
did you add this dll in another project and its still open?

Author

Commented:
aha, I did, how do I close it?
CERTIFIED EXPERT

Commented:
just close the project that you added it to

Author

Commented:
It is in the same project but in another dll
CERTIFIED EXPERT
Commented:
then recompile the whole project with the Dll included

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.