FileAttribues - PermissionTestNewsGroup.zip (0/1)

D

Daniel Garavaldi

Hi C# Gurus

Has anybody of you experience with FileAttributes?
I'm looking for alternatives and elegant solutions when opening a file
with FileInfo and query the file-attributes (read-only, archive ...)

What I do at the moment is mapping the fileInfo.Attributes to my
enum-type

private enum AttributeTypes {ReadOnly = 0, Normal = 1, Archived = 2,
Directory = 3};

By opening the file I get all the attributes into a string ....

FileInfo f = FileInfo("c:\hello.txt");
string attr = f.Attributes.ToString();


... and can of course parse it.


static void SetFileAttribueFlags(string attribute)
{
string attr = attribute.ToUpper();
//readonly
if(attr.IndexOf(FileAttributes.ReadOnly.ToString().ToUpper()) >= 0)
fileAttributes[(int)AttributeTypes.ReadOnly] = true;
//normal
if(attr.IndexOf(FileAttributes.Normal.ToString().ToUpper()) >= 0)
fileAttributes[(int)AttributeTypes.Normal] = true;
//archive
if(attr.IndexOf(FileAttributes.Archive.ToString().ToUpper()) >= 0)
fileAttributes[(int)AttributeTypes.Archived] = true;
//directory
if(attr.IndexOf(FileAttributes.Directory.ToString().ToUpper()) >= 0)
fileAttributes[(int)AttributeTypes.Directory] = true;


}


Any idea to do it with .NET-Attributes (f.e. [FlagsAttribute()] )

Looking forward reading something from you.

By the way: attached the complete code. The console program expects an
input parameter that means a full qualified filename.
 
J

Jay B. Harlow [MVP - Outlook]

Daniel,
I would just use FileAttributes.ReadOnly instead of converting it to an "r".

However! If I really did need to convert ReadOnly to "r", I would use a hash
table to map FileAttributes.ReadOnly to "r". I would have a second hash
table or array list that contains these abbreviated attributes or even just
a string or StringBuilder. Note the hash table is keyed by the FileAttribute
values themselves, no need to convert them to upper case strings.

Hashtable mapping = new Hashtable();
mapping.Add(FileAttributes.Archive, "a");
mapping.Add(FileAttributes.Normal, "n");
// include a mapping for each attribute you want to convert

The problem would seem to be 'extracting' each unique attribute out of
FileInfo.Attributes, I would use my first hash table to map bit masks (the
keys) that I check on FileInfo.Attributes.

ArrayList fileAttributes = new ArrayList();
FileInfo fi;
FileAttributes attributes = fi.Attributes;

foreach(DictionaryEntry de in mapping)
{
FileAttributes key = (FileAttributes)de.Key;
if ((attributes & key) == key)
{
// alternative de.Value could be a key into the second hash
table
fileAttributes.Add(de.Value);
}
}

This means that you do not need the AttributeTypes enum itself. Also your
code becomes significantly shorter ;-)

Hope this helps
Jay
 
D

Daniel Garavaldi

Hi Jay

Thank you very much for your answer. It's definetly a smooth solution that
use for my app.
In the meantime I played with CompareTo-Method, but the solution does not
work in all cases,
so I'll adapt your solution to my requirements.

Regards
Daniel


Jay B. Harlow said:
Daniel,
I would just use FileAttributes.ReadOnly instead of converting it to an "r".

However! If I really did need to convert ReadOnly to "r", I would use a hash
table to map FileAttributes.ReadOnly to "r". I would have a second hash
table or array list that contains these abbreviated attributes or even just
a string or StringBuilder. Note the hash table is keyed by the FileAttribute
values themselves, no need to convert them to upper case strings.

Hashtable mapping = new Hashtable();
mapping.Add(FileAttributes.Archive, "a");
mapping.Add(FileAttributes.Normal, "n");
// include a mapping for each attribute you want to convert

The problem would seem to be 'extracting' each unique attribute out of
FileInfo.Attributes, I would use my first hash table to map bit masks (the
keys) that I check on FileInfo.Attributes.

ArrayList fileAttributes = new ArrayList();
FileInfo fi;
FileAttributes attributes = fi.Attributes;

foreach(DictionaryEntry de in mapping)
{
FileAttributes key = (FileAttributes)de.Key;
if ((attributes & key) == key)
{
// alternative de.Value could be a key into the second hash
table
fileAttributes.Add(de.Value);
}
}

This means that you do not need the AttributeTypes enum itself. Also your
code becomes significantly shorter ;-)

Hope this helps
Jay

Daniel Garavaldi said:
Hi C# Gurus

Has anybody of you experience with FileAttributes?
I'm looking for alternatives and elegant solutions when opening a file
with FileInfo and query the file-attributes (read-only, archive ...)

What I do at the moment is mapping the fileInfo.Attributes to my
enum-type

private enum AttributeTypes {ReadOnly = 0, Normal = 1, Archived = 2,
Directory = 3};

By opening the file I get all the attributes into a string ....

FileInfo f = FileInfo("c:\hello.txt");
string attr = f.Attributes.ToString();


.. and can of course parse it.


static void SetFileAttribueFlags(string attribute)
{
string attr = attribute.ToUpper();
//readonly
if(attr.IndexOf(FileAttributes.ReadOnly.ToString().ToUpper()) >= 0)
fileAttributes[(int)AttributeTypes.ReadOnly] = true;
//normal
if(attr.IndexOf(FileAttributes.Normal.ToString().ToUpper()) >= 0)
fileAttributes[(int)AttributeTypes.Normal] = true;
//archive
if(attr.IndexOf(FileAttributes.Archive.ToString().ToUpper()) >= 0)
fileAttributes[(int)AttributeTypes.Archived] = true;
//directory
if(attr.IndexOf(FileAttributes.Directory.ToString().ToUpper()) >= 0)
fileAttributes[(int)AttributeTypes.Directory] = true;


}


Any idea to do it with .NET-Attributes (f.e. [FlagsAttribute()] )

Looking forward reading something from you.

By the way: attached the complete code. The console program expects an
input parameter that means a full qualified filename.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top