rainshark
rainshark

Reputation: 45

How to modify the attributes of a file with no extension in C#?

This is my code

static string pat = "C:\\" + System.Environment.UserName + 
              "\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\History";
FileInfo hist = new FileInfo(pat);

Here History is a file, with no extension. All I want to do is this:

hist.IsReadOnly = true;

But directorynotfoundexception comes. Please help me, how do I access the file, it thinks that History is a directory!

Upvotes: 2

Views: 118

Answers (2)

HABJAN
HABJAN

Reputation: 9338

Your path is strange. It starts like "C:\Username\AppData...".

I think what you are looking for is something like this:

string path = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
path = System.IO.Path.Combine(path, "Google\\Chrome\\User Data\\Default\\History");

The result will look like this (on XP):

C:\Documents and Settings\Username\Local Settings\Application Data\Google\Chrome\User Data\Default\History

Windows 7 will give you different result.

This has to work for file with or without extension:

FileInfo fi = new FileInfo(path);
fi.Attributes |= System.IO.FileAttributes.ReadOnly;

And this has to work for directory:

DirectoryInfo di = new DirectoryInfo(path);
di.Attributes |= System.IO.FileAttributes.ReadOnly;

Upvotes: 5

Iain Ward
Iain Ward

Reputation: 9946

I think your directory is wrong, you seem to be missing the 'Users' folder. It should be:

static string pat = "C:\\Users\\" + System.Environment.UserName + ...

Upvotes: 1

Related Questions