在“隐藏"文件夹中创建XML文件. [英] Create XML file in Hidden folder.

查看:66
本文介绍了在“隐藏"文件夹中创建XML文件.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好.我在隐藏文件夹中创建xml文件时遇到问题.创建隐藏文件夹没有问题

Hello. I have an problem creating xml files in hidden folders. There''s no problem creating the hidden folder

string path = setting.FileLocation + @"\Setting"; // or whatever
          if (!Directory.Exists(path))
          {
              DirectoryInfo di = Directory.CreateDirectory(path);
              di.Attributes = FileAttributes.Directory | FileAttributes.Hidden;
          }




但是它不允许我为它创建xml文件.
我正在使用XmlTextWriter使其保持简单.




But it wont allow me to create the xml file to it.
I''m using the XmlTextWriter to keep it simple.

public void AddSetting(Settings setting)
       {
           string path = setting.FileLocation + @"\Setting"; // or whatever
           if (!Directory.Exists(path))
           {
               DirectoryInfo di = Directory.CreateDirectory(path);
               di.Attributes = FileAttributes.Directory | FileAttributes.Hidden;
           }
           XmlTextWriter textWriter = new XmlTextWriter(path, null);

           // Opens the document
           textWriter.WriteStartDocument();

           // Write comments
           textWriter.WriteComment("Setting for FileLocation");

           // Write first element
           textWriter.WriteStartElement("FileLocation");

           // Write next element
           textWriter.WriteStartElement("FileLocation", ""); textWriter.WriteString(setting.FileLocation);
           textWriter.WriteEndElement();

           // Ends the document.
           textWriter.WriteEndDocument();
           // close writer
           textWriter.Close();
       }


它还给了我这个错误消息拒绝访问路径"C:\ Setting".
该路径应在下一个XmlTextWriter执行之前创建.
最好的问候Morten Starck


It also gives me this error message Access to the path ''C:\Setting'' is denied.
The path should be created before the next XmlTextWriter is executed.
Best Regard Morten Starck

推荐答案

除非您有尚未发布的代码,否则XmlTextWriter使用相同的 path 变量,它指向一个目录,并且不包含文件名
Unless you''ve got code you''ve not posted, your XmlTextWriter is using the same path variable, which is pointing to a directory, and does not contain a filename


MaeqW为您找到了一个错误.

我可以看到更多问题.
您基于对工作文件夹的某些假设来使用相对路径Setting目录.这是完全错误的.工作文件夹可以在任何位置,这与程序集所在的文件夹不同,也与您测试应用程序所在的文件夹不同.您的客户可以从任何其他文件夹启动应用程序.其次,您认为可以在此目录中写入的假设是完全错误的.在其他运行方式下,它可以是只读的!

对于父目录,请使用以下内容:

MaeqW found one bug for you.

I can see more problems.
You use you relative-path Setting directory based on some assumption on you working folder. This is completely wrong. A working folder can be anywhere, this is not the same folder where your assembly is and not where you tested the application. Your customer can start application from any other folder. Secondly, your assumption that you can write in this directory is completely wrong. In other run it can be read-only!

For a parent directory use something like this:

string userPath =
   System.Environment.GetFolderPath(
       System.Environment.SpecialFolder.LocalApplicationData);



有关更多信息和其他选项,请参见System.Environment.SpecialFolder.

最后,您使用hidden属性的想法太幼稚了!大多数文件管理器和其他软件都可以轻松,轻松地让用户看到它.
为了获得更好的保护,请考虑Isolated Storage( http://msdn.microsoft.com/en-我们/library/system.io.isolatedstorage.aspx [ ^ ]).

—SA



See System.Environment.SpecialFolder for more information and other options.

Finally, you idea of using hidden attribute is so naive! Most file managers and other software can easily, easily let the user to see it.
For better protection consider Isolated Storage (http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.aspx[^]).

—SA


这篇关于在“隐藏"文件夹中创建XML文件.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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