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\\His开发者_StackOverflow中文版tory";
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!
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;
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 + ...
精彩评论