tags:

views:

411

answers:

5

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?

A: 

If that's an option for you you could try to use File.SetAttributes to remove the hidden attribute temporarily, do your work and then set it back to the previous state.

Thomas Wanner
+4  A: 

Took this answer from: http://www.dotnetspark.com/Forum/314-accessing-hidden-files-and-write-it.aspx

1- Set File as Visible so it can be overwritten

// Get file info
FileInfo myFile= new FileInfo(Environment.CurrentDirectory + @"\hiddenFile.txt");

// Remove the hidden attribute of the file
myFile.Attributes |= FileAttributes.Normal;

2- Make changes to the file

// Do foo...

3- Set back file as hidden

// Put it back as hidden
myFile.Attributes |= FileAttributes.Hidden;
Pierre-Luc Champigny
You shouldn't override `myFile.Attributes` like that. It's a bit mask, so set only the one bit. `myFile.Attributes |= FileAttributes.Hidden`.
Sam
Due to the wrong usage of setting attribute flags and the wrong approach to the problem in general, I don't understand why this answer is getting upvoted so much...
Lucero
A: 

You can unhide the file before writing into it and after complete writing hide it again.

viky
A: 

Once you've opened a file, you can change its attributes (including to readonly) and continue writing to it. This is one way to prevent a file from being overwritten by other processes.

So it would seem to be possible to unhide the file, open it, then reset it to hidden, even while you've got it open.

For example, the following code works:

public void WriteToHiddenFile(string fname)
{
    TextWriter    outf;
    FileInfo      info;  

    info = new FileInfo(fname);
    info.Attributes = FileAttributes.Normal;    // Set file to unhidden
    outf = new StreamWriter(fname);             // Open file for writing
    info.Attributes = FileAttributes.Hidden;    // Set back to hidden
    outf.WriteLine("test output.");             // Write to file
    outf.Close();                               // Close file
}

Note that the file remains hidden while the process writes to it.

Loadmaster
+1  A: 

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.

using (FileStream fs = new FileStream(filename, FileMode.Open))
using (TextWriter tw = new StreamWriter(fs)) {
  tw.WriteLine("foo");
  tw.Close();
}
Lucero