如何在C#中隐藏文件?

Roh*_*hit 29 .net c#

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

我怎么能隐藏它?

sto*_*roz 51

接受的答案是:

File.SetAttributes(path, FileAttributes.Hidden);
Run Code Online (Sandbox Code Playgroud)

会导致某些其他属性丢失,所以你应该:

File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
Run Code Online (Sandbox Code Playgroud)

  • @omJohn8372:不!按位与将*清除除“隐藏”之外的所有标志。您正在寻找的是按位异或 (`^`)。 (2认同)

Raj*_*sh 33

File.SetAttributes("pathToFile",FileAttributes.Hidden)
Run Code Online (Sandbox Code Playgroud)


BFr*_*ree 6

    FileInfo f = new FileInfo(myFileName);
    f.Attributes = FileAttributes.Hidden;
Run Code Online (Sandbox Code Playgroud)