Writing to read-only files
I wrote a program (c#.net 2.0)that looks through all ini files within a given directory, and attempts to replace some string patterns if found. Some of these .ini files are read-only, and there is a vast amount of them.
Any suggestions on how best to handle this? It is going to be used in a win2k, win2k3 32bit environm开发者_如何学Cent with up to .net 2.0 installed.
Change the readonly attribute (you can only do this if you have the correct permissions):
FileInfo myFile = new FileInfo(pathToFile);
myFile.IsReadOnly = false;
The file is now writable - see the documentation on the IsReadOnly
property.
Just clear the ReadOnly attribute
File.SetAttributes(filePath, File.GetAttributes(filePath) & ~FileAttributes.ReadOnly);
but first check if it's there
bool isReadOnly = (File.GetAttributes(FilePath) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly;
精彩评论