Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write to a hidden file?

Tags:

c#

file

hidden

I am using the TextWriter to try to write to a hidden file, and it is throwing an exception. I can't seem to figure out how to write to a hidden file.

using (TextWriter tw = new StreamWriter(filename)) {     tw.WriteLine("foo");     tw.Close(); } 

Exception:

Unhandled Exception: System.UnauthorizedAccessException:  Access to the path 'E:\*\media\Photos\2006-08\.picasa.ini' is denied. 

How can I write to a hidden file?

like image 634
esac Avatar asked Feb 11 '10 19:02

esac


People also ask

Which command is used to display hidden files?

Using the command line command dir /ah displays the files with the Hidden attribute.

What does it mean for a file to be hidden?

A hidden file is a file which has the hidden attribute turned on so that it is not visible to users when exploring or listing files. Hidden files are used for storage of user preferences or for preservation of the state of utilities. They are created frequently by various system or application utilities.

How do you find out a hidden file in your computer?

Select the Start button, then select Control Panel > Appearance and Personalization. Select Folder Options, then select the View tab. Under Advanced settings, select Show hidden files, folders, and drives, and then select OK.

How do hidden files work?

A hidden file is any file with the hidden attribute turned on. Just as you'd expect, a file or folder with this attribute toggled on is invisible while browsing through folders—you can't see any of them without explicitly allowing all of them to be seen.


1 Answers

It seems that the problem is that kind of a File.Exists() check is done internally, which fails if the file is hidden (e.g. tries to do a FileMode.Create on a file which already exists).

Therefore, use FileMode.OpenOrCreate to make sure that the file is opened or created even if it is hidden, or just FileMode.Open if you do not want to create it if it doesn't exist.

When FileMode.OpenOrCreate is used though, the file will not be truncated, so you should set its length at the end to make sure that there is no leftover after the end of the text.

using (FileStream fs = new FileStream(filename, FileMode.Open)) {   using (TextWriter tw = new StreamWriter(fs)) {     // Write your data here...     tw.WriteLine("foo");     // Flush the writer in order to get a correct stream position for truncating     tw.Flush();     // Set the stream length to the current position in order to truncate leftover text     fs.SetLength(fs.Position);   } } 

If you use .NET 4.5 or later, there is a new overload which prevents the disposal of the StreamWriter to also dispose the underlying stream. The code could then be written slighly more intuitively like this:

using (FileStream fs = new FileStream(filename, FileMode.Open)) {   using (TextWriter tw = new StreamWriter(fs, Encoding.UTF8, 1024, true)) {     // Write your data here...     tw.WriteLine("foo");   }   // Set the stream length to the current position in order to truncate leftover text   fs.SetLength(fs.Position); } 
like image 77
Lucero Avatar answered Sep 22 '22 00:09

Lucero