用c#.net隐藏文本文件 [英] hiding text file with c#.net

查看:190
本文介绍了用c#.net隐藏文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写文本文件,并将该文本文件的属性设置为"System","ReadOnly","Archive","Hide".

您可以通过右键单击文件,然后选中/取消选中文件的属性来进行操作.

我如何使用C#做到这一点.

I need to write text file and want to set the attributes of that text file to "System" , "ReadOnly" , "Archive" , "Hide".

As you can do by right clicking on the file and check/uncheck the attributes of file.

How I can do this using C#.

推荐答案

为此使用FileAttribute
Use the FileAttribute for this
File.SetAttributes("pathToFile",FileAttributes.Hidden)


请参阅以下链接
http://www.csharp-examples.net/file-attributes/ [


see the below links
http://www.csharp-examples.net/file-attributes/[^]
Thanks
--RA


这些操作完全是幼稚的.您可以隐藏文件或目录,任何其他代码都可以显示它.它根本不提供任何保护.实际上,几乎所有文件管理器都为您提供查看所有文件的选项.例如,即使是只读文件,也仅在发出一条警告后便被删除.这样,使用属性进行操作就毫无意义.您可以认为文件系统功能的这一部分几乎"已过时.

除非您使用权限,否则您将对此无能为力.您可以从这里开始:
http://msdn.microsoft.com/en-us/library/aa720406%28v = vs.71%29.aspx [ ^ ].

另请参阅以下讨论: http://social. msdn.microsoft.com/forums/zh-CN/csharpgeneral/thread/c513ca26-9bf8-4e39-a993-4ebf90aaece6/ [ http://msdn.microsoft.com/en-us/library/3ak841sy.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. io.isolatedstorage.isolatedstoragefile.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/cc221360%28v = vs.95%29.aspx [ ^ ].

另请参见此CodeProject文章: .NET中用于存储应用程序的隔离存储数据 [ ^ ].

—SA
These manipulations are totally naive. You can hide the file or directory, any other code can show it. It does not provide any protection at all. In fact nearly all File Managers routinely provide you with the option to see all the files. Even read-only files are, say, deleted, after just one warning. In this way, manipulations with attributes simply make no sense. You can consider this part of file system functionality "almost" obsolete.

You can do nothing about this unless you use permissions. You can start from here:
http://msdn.microsoft.com/en-us/library/aa720406%28v=vs.71%29.aspx[^].

See also this discussion: http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/c513ca26-9bf8-4e39-a993-4ebf90aaece6/[^].

However, I would advice not doing it. Messing up with permission and hide anything, taking the risk to leave hidden files forever due to different mistakes? Looks childish. Consider 1) doing everything on an open way, which might be your best choice; 2) using encryption (which is much stronger and more robust then any system tricks taken together, 3) using resources embedded in an executable; 4) using isolated storage if applicable.

On isolated storage, please see:
http://msdn.microsoft.com/en-us/library/3ak841sy.aspx[^],
http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile.aspx[^],
http://msdn.microsoft.com/en-us/library/cc221360%28v=vs.95%29.aspx[^].

See also this CodeProject article: Isolated Storage in .NET to store application data[^].

—SA


这篇关于用c#.net隐藏文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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