Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c中创建隐藏的Xml文件#_C#_Xml_Hidden - Fatal编程技术网

C# 在c中创建隐藏的Xml文件#

C# 在c中创建隐藏的Xml文件#,c#,xml,hidden,C#,Xml,Hidden,我创建了一个XML文件,但问题是我需要它是一个隐藏的文件 现在我看到文件夹中的XML文件,可以单击它等等 我的代码:(这是xml文件的创建) 如何更改它?在保存文件之前,如果文件是隐藏的或只读的,则必须先修复(取消隐藏)权限。所以你需要这样做: FileAttributes attributes = File.GetAttributes(xmlPath); // Hide the file. File.SetAttributes(path, File.GetAttributes(pa

我创建了一个XML文件,但问题是我需要它是一个隐藏的文件

现在我看到文件夹中的XML文件,可以单击它等等

我的代码:(这是xml文件的创建)


如何更改它?

在保存文件之前,如果文件是隐藏的或只读的,则必须先修复(取消隐藏)权限。所以你需要这样做:

  FileAttributes attributes = File.GetAttributes(xmlPath);
  // Hide the file.
  File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
  Console.WriteLine("The {0} file is now hidden.", path);
private void RemoveHiddenNReadOnly()
{
    if (File.Exists) // File here is the FileInfo of the xml file for the class
    {
        File.Attributes &= ~FileAttributes.Hidden; // Remove Hidden Flag 
        File.Attributes &= ~FileAttributes.ReadOnly; // Remove ReadOnly Flag
    }
}
然后保存文件,然后再次设置隐藏标志

doc.Save(File.FullName); 
File.Attributes |= FileAttributes.Hidden;

本例中的“data.xml”放在调试文件夹中,但您可以将其更改为文件所在的路径->String path=“C:\Users\User\App….\filename.ext”

您希望它如何隐藏?就普通文件系统而言?@JonSkeet发布答案,我喜欢投我的第2票u@Rakitić:OP没有澄清问题,我怎么回答?我们不知道他们为什么要把它藏起来,或者它必须有多安全。如果一个简单的隐藏文件就足够了,那也没关系——但对于任何重要的事情来说,这都是相当糟糕的保护。@BassamAlugili:不,我不会回答一个问得不好的问题;这样鼓励用户不是一个好主意。您可能不希望新用户提出好的问题,但我希望将其保持在一个合理的标准。@BassamAlugili:不,我仍然不会回答一个太模糊而无法很好回答的问题。谢谢您的回答,这对我帮助很大。我非常感谢您的帮助。欢迎您将其标记为回答plz,如果它真的帮助你解决问题。
doc.Save(File.FullName); 
File.Attributes |= FileAttributes.Hidden;
FileAttributes attributes = File.GetAttributes("data.xml");

//To Hide the file
File.SetAttributes("data.xml", File.GetAttributes("data.xml") | FileAttributes.Hidden);

//and to unhide the file
if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
{
    attributes &= ~FileAttributes.Hidden;
    File.SetAttributes("data.xml", attributes);
}