我怎样写一个隐藏的文件? [英] How do I write to a hidden file?

查看:225
本文介绍了我怎样写一个隐藏的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的TextWriter尝试写一个隐藏的文件,它抛出一个异常。我似乎无法弄清楚如何写一个隐藏的文件。

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?

推荐答案

编辑2:这个答案解决问题,而不是处理问题的正确方法。你应该寻找卢塞罗的回答。

EDIT 2: This answer solve the problem, but is not the correct way to deal with the problem. You should look for Lucero's answer.

把这个回答:的 http://www.dotnetspark.com/Forum/314-accessing-hidden-files-and-write-it.aspx

1 - 设置文件为可见,因此它可以被覆盖

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.Hidden;



2 - 进行更改文件

2- Make changes to the file

// Do foo...

3折戟文件为隐藏

// Put it back as hidden
myFile.Attributes |= FileAttributes.Hidden;



编辑:我固定在我的回答一些问题,通过briler

I fixed some problem on my answer as mentionned by briler

这篇关于我怎样写一个隐藏的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆