473,386 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

FileAttribues - PermissionTestNewsGroup.zip (0/1)

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().T oUpper()) >= 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.

Nov 15 '05 #1
2 1448
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" <da**************@gmx.net> wrote in message
news:um********************************@4ax.com...
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().T oUpper()) >= 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.


Nov 15 '05 #2
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 [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:ec**************@TK2MSFTNGP11.phx.gbl...
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" <da**************@gmx.net> wrote in message
news:um********************************@4ax.com...
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().T oUpper()) >= 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.



Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.