Link to home
Start Free TrialLog in
Avatar of cintra
cintra

asked on

How can I Access and modify custom attributes of a file

Hi all,

I'm building a little application to manage some files over a network with many users, the idea is that I wan't to prevent more than one person accessing the file at one time. All these files have the same name (but will sit in different folders) so I need to add a 'Tag' or 'Meta Data' to the file to allow me to identify it.

Through Windows you can right click on a file, Properties and Summary and then you can type in values to attach to the file. I need to access and modify this using C# has anyone ever tried this before??

Any help would be great!!
Avatar of patrickriva
patrickriva
Flag of Italy image


            var stream = new FileStream("<fullpath>", FileMode.Open, FileAccess.Read, FileShare.None);

//use your file it, it'll be locked to all others applications

            stream.Close();
Avatar of cintra
cintra

ASKER

Thanks, but maybe I didn't explain myself properly. Because all the files will be downloaded to each of the clients when they run this application - the first thing I need to is to upload them back to the server. To find out which folder I need to upload the files to I need to add some extra data to the file like a tag or something.

Is it possible to set an attribute of the file to hold this information so that I when the file is on the client I know where it came from?

I noticed when using Windows XP I can manually add information to a file by right clicking on it, selecting Properties and Summary - I can then enter data that attaches itself to the file, this would do what I need if I could access it programatically
ASKER CERTIFIED SOLUTION
Avatar of Anurag Thakur
Anurag Thakur
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cintra

ASKER

Thanks again but not quite what I'm after...

The problem with that code (below) is it allows you to change the readonly attribute of the file, which is great, but what I need to do is attach a string as an attribute to allow me to identify the file and know where it has come from.

Kind of like a custom attribute.

            Try
                If (Filename.Attributes And FileAttributes.ReadOnly) Then
                    Filename.Attributes = (Filename.Attributes And Not FileAttributes.ReadOnly)
                End If
            Catch E As Exception
                Console.WriteLine("Error changing attribute for {0}", Filename.FullName)
                Console.WriteLine("Error: {0}", E.Message)
            End Try

Open in new window

Avatar of cintra

ASKER

That second link you sent me seems like its going to do the trick, only little problem and maybe its just me being stupid but he does mention having to reference a DLL - how do you this?

I've downloaded the DSOFile thing and it contains a dll but not sure how to to include it.

Any Ideas?
install the exe and the right click on the project and select add references and find the reference for Microsoft Developer Support OLE File Property in either .net section or com section
How about this ... when the file gets down to the users machine generate a small 8 byte text file with a code in it. Then run the dos command  copy clientfile.file + code.txt clientfile.file

What this does is append the text file to the end of your file.

You then upload the file and look at the last 8 bytes of the file and Voila there is your code no messing with attributes.

wc