Connecting Tech Pros Worldwide Forums | Help | Site Map

FileAttribues - PermissionTestNewsGroup.zip (0/1)

Daniel Garavaldi
Guest
 
Posts: n/a
#1: Nov 15 '05
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.






Jay B. Harlow [MVP - Outlook]
Guest
 
Posts: n/a
#2: Nov 15 '05

re: FileAttribues - PermissionTestNewsGroup.zip (0/1)


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" <daniel.garavaldi@gmx.net> wrote in message
news:umhlovoadikl8eapu052or5n052e37128b@4ax.com...[color=blue]
> 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.
>
>
>
>
>[/color]


Daniel Garavaldi
Guest
 
Posts: n/a
#3: Nov 15 '05

re: FileAttribues - PermissionTestNewsGroup.zip (0/1)


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]" <Jay_Harlow@email.msn.com> wrote in message
news:ecpHleakDHA.3504@TK2MSFTNGP11.phx.gbl...[color=blue]
> Daniel,
> I would just use FileAttributes.ReadOnly instead of converting it to an[/color]
"r".[color=blue]
>
> However! If I really did need to convert ReadOnly to "r", I would use a[/color]
hash[color=blue]
> table to map FileAttributes.ReadOnly to "r". I would have a second hash
> table or array list that contains these abbreviated attributes or even[/color]
just[color=blue]
> a string or StringBuilder. Note the hash table is keyed by the[/color]
FileAttribute[color=blue]
> 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" <daniel.garavaldi@gmx.net> wrote in message
> news:umhlovoadikl8eapu052or5n052e37128b@4ax.com...[color=green]
> > 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.
> >
> >
> >
> >
> >[/color]
>
>[/color]


Closed Thread