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

5

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 don't know what attribute X means.

So, what is attribute X and how can one set/clear it on the selected file in C#?

Sita answered 23/12, 2011 at 15:19 Comment(8)
It seems that all comments got lost - I was asked if attribute X would disappear after performing chkdsk. No, I have done the chkdsk - no problems were found and attribute X is still there.Sita
I would guess at an intentionally invalid file attribute. There are two unassigned bits in FAT32, 0x40 and 0x80. This is just a guess.Scurrile
I suspect Hans is right. Get the file attributes, cast it to int, and output it in hex. Then compare the set bits to the File Attribute Constants: msdn.microsoft.com/en-us/library/windows/desktop/…. That should tell you which bit is improperly set. If you want to clear the attribute, clear it in that int, cast the int back to a FileAttributes, and call File.SetAttributes.Stagnate
I followed Jim's advice (thanks for step-by-step instructions) and first tried getting and setting attributes for dummy file. Got 0x20 (archive file), then I set it to 0x22 and dummy became hidden. Tried to get attributes of file in question - got 0x42 (device+hidden). Tried to set it to 0x2 (just hidden), but I got stopped by "Access denied". Then I tried to set dummy's attributes to 0x42. No problems there, however when I check dummy's Details tab, it's only attribute is H. And if I get dummy's attributes now, it's just 0x2. "0x40 Reserved; do not use". Any other way?Sita
See SetFileAttributes function, section Remarks. I know what do not use means, but this file had set 0x40 attribute by other program, I'd like to be able to do the same. Or to set it off.Sita
Update: this might be of some help.Sita
I'm not sure if you need to do this in Windows. If not, my answer might help.Cupellation
@mootinator doing this in Windows is mandatory for me.Sita
R
1

Maybe I'm wrong but... I think that X attribute means that the file has extended attributes. Unfortunately, extended attributes can't be modified using C#.

Raffin answered 23/12, 2011 at 18:32 Comment(3)
I think you might be right about extended attributes. I now know that FILE_ATTRIBUTE_DEVICE 0x40 can't be set using SetFileAttribute, but maybe there is another method to set or clear this attribute using C#. I've read about EA DATA. SF, but it seems like I have much more reading to do. Any idea how to access and edit EA DATA. SF file from C#? Implementation of EA on FAT, EA - what and howSita
I tried few file removing tools available out there, but none of them helped to get rid of the file. As for EA DATA. SF, I'm not sure that this EA file is on my flash USB drive. How can check for it's existence? It's said here that EA DATA. SF file can only be deleted outside of Windows because of its "in use" status. Is that really a case? And if I can't remove file with attr X, because of EA DATA. SF, which I also can't remove because of win7... How can I make my own EA DATA. SF after formatting flash drive?Sita
The reason I asked how to check for EA DATA. SF file existence was because it doesn't show up in explorer (I've set showing hidden and system files on). I've tried if exist "EA DATA. SF" echo It's here! in cmd, but I didn't get confirmation. Looks like it isn't EA DATA. SF file that makes the other file untouchable. I might be wrong, would appreciate help.Sita
C
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.

Cupellation answered 21/10, 2012 at 2:51 Comment(2)
Thank you for your answer, however I need to do this in Windows.Sita
Ah. Of course, you might actually be able to get this code working under Cygwin if you're still stuck.Cupellation
L
1

File Attribute X correspondes to System.IO.FileAttributes.Device which has an integer value of 64 (0x40) using .NET you cannot directly set this value. The problem is that when you call File.SetAttributes(path, fileAttributes) it calls an internal dll import function in Mscorlib.dll, namely the static method Microsoft.Win32.Win32Native.SetFileAttributes, this is a direct dll import of kernel32.dll and is defined as:

// Microsoft.Win32.Win32Native
[DllImport("kernel32.dll", BestFitMapping = false, CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool SetFileAttributes(string name, int attr);

So put simply, even using kernel32.dll you cannot set this value. See: SetFileAttributes, and System.IO.FileAttributes.

But, you can write code that will effectively do the same as setting or removing this attribute.

Your best and likely only (reasonable) answer would be to keep a template file with the X permission, when you need to set a file to have X you rename the existing file, copy the template X file and then write the other attributes.

For removing the X permission you should read the file and write to a new file and then copy there permissions using a simple call to File.SetAttributes(path, File.GetAttributes(oldPath)); (which won't set the X permission.

This approach is pretty straight forward and would be entirely possible in native .NET (and wouldn't actually look too nasty either, compared to hacking around at the disk level or invoking cgywin - if that would even work).

Lunate answered 14/11, 2013 at 10:55 Comment(0)
T
0

The X is most likely the execute permissions. icalcs mentions it and you can possibly use this command from C# to set a file attribute, although I am not sure if it will work against a FAT filesystem or not.

Trim answered 23/12, 2011 at 18:48 Comment(2)
I don't think that "execute" attribute applies to a Windows file-system like FAT.Raffin
I've read about icacls, tried to use it on file in question, got "Acces denied". Then I experimented a little bit with icacls on other files on USB stick, but didn't get very far: when I typed icacls dummy all I get is dummy D:NO_ACCESS_CONTROL. I couldn't make any changes to ACL of dummy, even though I tried /grant, /deny and /remove. Then I did some searching and found that "acls are first supported by ntfs file system" according to andreas2610 postSita
E
0

I cannot reproduce your issue but as workaround to remove that attribute I think you can try with robocopy:

robocopy xattributefile copyoffile /copy:DT

/copy:DT: Specifies the file properties to be copied.

This should not copy attribute but i don't know if it works with X attribute)

Eritrea answered 12/11, 2013 at 12:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.