MAW74656
MAW74656

Reputation: 3539

How can I access file properties in C#?

This may actually be a 2-part question.

1) How can I access properties of a file (read-only status, creationdate, pretty much anything you can see on the properties screen when you right-click a file and select "Properties") in C#?

Is there a syntax like file.Attributes["CreationDateTime"] that will do this?

2) How can I access the target of a .lnk file (a desktop shortcut for instance) using C#?
And I want the entire target, I have ones which look like:

"[somepath]" "[somepath]" /cmd "somepath"

And I need that entire value. Current code is giving only the first path.

Upvotes: 0

Views: 2444

Answers (2)

gideon
gideon

Reputation: 19465

Chris has answered the first part.

For the second part of your question : You're actually looking to play with IShellLink which is native Windows API that is not available directly in .NET.

However, someone out there always writes a reusable library. I should have a property that gives you the target.

Heres another library I found at Wikipedia.org this one is recommended since it is a 100% managed solution, IOW, it does not call COM (something you'd rather not do)

Finally just incase you need the file format, here it is. The pdf link doesn't seem to be working, but you can download all revisions I think.


Since you're not clear on the first question too, I would be nice if you said exactly what information you need from the file (Like when you said you wanted the target)

Like IShellLink, if .NET doesn't provide some API, you have to go native. But usually .NET does provide what you need. Look at this paragraph on the msdn page that Chris has linked to:

The FileInfo class provides the following properties that enable you to retrieve information about a file. For an example of how to use each property, see the property pages.

The Directory property retrieves an object that represents the parent directory of a file.

The DirectoryName property retrieves the full path of the parent directory of a file.

The Exists property checks for the presence of a file before operating on it.

The IsReadOnly property retrieves or sets a value that specifies whether a file can be modified.

The Length retrieves the size of a file.

The Name retrieves the name of a file.

Upvotes: 2

Chris B. Behrens
Chris B. Behrens

Reputation: 6297

With the System.IO.FileInfo object. It has creation time as a property.

I'm not sure what you mean for the second part...

Upvotes: 3

Related Questions