Alex Rivera | Logout

How to set clear attribute "X" on files with in C#?

Asked 2011-12-23T15:19:46.570
24

I have a hidden file on my USB removable storage (FAT file system).

I'm using Windows 7. If I go to Properties window of this file and then to the Details tab, I will see that the attributes of the file are HX. When I run cmd and use attrib, I was told that file has attribute H. Nothing about X. I've tried help attrib, but still nothing about attribute X.

I know that:

  • H = Hidden
  • S = System
  • A = Archival
  • R = Readonly

However, I have no idea what X stands for. I've also found what the N and E attributes are for.

Wikipedia has no mention of what the X attribute is.

Google found two topics mentioning attribute X:

None of these helped.

I'd like to be able to read/write this flag in C#, but so far I read the documentation for the FileAttributes enumeration and experimented with setting some of listed attributes (using File.GetAttributes & File.SetAttributes methods) on the file.

None of these experiments resulted in attribute X showing up in Properties -> Details. So, I have no idea how to approach it, especially since I still d

Edit
Report

1 Answer

1

Unfortunately, the Windows API will prevent you from setting/unsetting FILE_ATTRIBUTE_DEVICE because it isn't meant to be set on files. If you really need the ability to do that you'll have to access the disk directly. I don't really recommend trying to do that in C#.

The quickest possible implementation would probably be to forget about doing it in Windows, download the source for mtools and make a couple of edits to the mattrib source in order to get it working.

eg add to msdos.h:

#define ATTR_DEVICE 0x40
#define IS_DEVICE(entry) (HAS_BIT((entry),ATTR_DEVICE))

Then add the code to mattr.c so you can set the attribute and verify the change:

static int view_attrib(direntry_t *entry, MainParam_t *mp)
{
    ...
    /* Add this if block */
    if(IS_DEVICE(entry))
        putchar('X');
    ...
}


static int concise_view_attrib(direntry_t *entry, MainParam_t *mp)
{
    ...
    /* Add the following if block */
    if(IS_DEVICE(entry))
        putchar('X');
    ...
}

static int letterToCode(int letter)
{
    switch (toupper(letter)) {
        ...
        /* Add the following case */
    case 'X':
        return ATTR_DEVICE;
        ...
    }
}

Then you would just have to setup your drive on mtools and call your newly created mattrib -x command on the file you wish to change.

answered 2012-10-21T02:51:29.567

Your Answer