Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

SetFileAttributes (kernel32)
 
.
Summary

C# Signature:

[DllImport("kernel32.dll")]
static extern bool SetFileAttributes(string lpFileName, uint dwFileAttributes);

Alternative C# Signature:

[DllImport("kernel32.dll")]
static extern bool SetFileAttributes(
     string lpFileName,
     [MarshalAs(UnmanagedType.U4)] FileAttributes dwFileAttributes);

User-Defined Types:

None.

You can create this FileAttributes type to go in your class and be used with the alternative signature if you wish (or you can use the System.IO.FileAttributes as displayed in the below example):

[Flags] public enum FileAttributes : 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
}

Notes:

This method works for Alternate Data Streams, also great when used with the Unicode header \\?\ to change the attributes of files who have paths to long to be dealt with via the shell.

Tips & Tricks:

Please add some!

Sample Code:

Using the "alternative" method above you can make calls like so:

SetFileAttributes("pathname", FileAttributes.System | FileAttributes.Hidden | FileAttributes.Archive);

Alternatively you can use like this to deal with unicode paths around 32,700 characters long:

private const String UnicodeHeader = @"\\?\";

[DllImport("kernel32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
private static extern bool SetFileAttributesW(string lpFileName, FileAttributes dwFileAttributes);

public static void SetFileAttributes(String path, FileAttributes dwFileAttributeFlags)
{
      if (!SetFileAttributesW(UnicodeHeader + path, dwFileAttributeFlags))
     {
             throw (Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error()));
     }
}

In the above example I have specified the W (Unicode) version of the function as opposed to the A (ANSI) version of the function and so I don't think I needed to specify the CharSet to be Unicode however I have done so for illustration purpose to show how you would force the non W version of the function to work with unicode (long) file paths. (Another user's edit: I tried this, and I think you still need to specify the CharSet.)

Alternative Managed API:

System.IO.File.SetAttributes(String path, System.IO.FileAttributes fileAttributes);

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions