如何在C#中隐藏文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1199571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:43:40  来源:igfitidea点击:

How to hide file in C#?

c#.net

提问by Rohit

I want to hide a file in c#. I know the file path and can create a FileInfo object.

我想在 c# 中隐藏一个文件。我知道文件路径并且可以创建一个 FileInfo 对象。

How can I hide it?

我怎样才能隐藏它?

采纳答案by stovroz

The previously accepted answer:

之前接受的答案:

File.SetAttributes(path, FileAttributes.Hidden);

will result in certain other attributes it may have being lost, so you should:

将导致它可能丢失的某些其他属性,因此您应该:

File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);

回答by BFree

    FileInfo f = new FileInfo(myFileName);
    f.Attributes = FileAttributes.Hidden;

回答by Rajesh

File.SetAttributes("pathToFile",FileAttributes.Hidden)

回答by Jeremy Cron

Try something like this:

尝试这样的事情:

FileInfo fi = new FileInfo(somefile);                
fi.Attributes = FileAttributes.Hidden;

回答by Toye_Brainz

The FileInfohidden attributes might be somewhat useless if the user's windows is set to display hidden files. It is more advisable to move file to a new path especially where user don't really navigate to like C:\Program Files\Common Filesor any path you might feel your user show lesser interest of visiting before you hide the file.

FileInfo如果用户的窗口设置为显示隐藏文件,隐藏属性可能有点用处。更可取的做法是将文件移动到新路径,尤其是在用户并未真正导航到喜欢的 C:\Program Files\Common Files路径或您可能认为用户在隐藏文件之前表现出较少访问兴趣的任何路径的情况下。