将文件保存在项目中的指定文件夹中

编程入门 行业动态 更新时间:2024-10-22 07:37:55
本文介绍了将文件保存在项目中的指定文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我创建一个XML文件,我想将它保存在指定文件夹我的项目里面的解决方案,我可以在Solution Explorer访问之内。

I am creating an Xml file and I want to save it in a specified folder inside my project within the solution where I can access it in the Solution Explorer.

我怎样才能使创建的文件夹和XML文件保存在里面指定的路径?

How can I specify the path so that a folder is created and the xml file saved inside it?

由于当时是它会在根目录下的文件。在Solution Explorer中我的项目,我无法查看

As it is at the moment it creates the file in the root directory of my project and I cannot view it in Solution Explorer.

XmlSerializer serializer = new XmlSerializer(typeof(BoxSet)); TextWriter textWriter = new StreamWriter("../../Box.xml");

不好意思,还是个新手...我失去了一些东西。

Sorry, still a newbie...am I missing something.?

推荐答案

在声明一个新的StreamWriter您可以指定路径。

You can specify the path when you declare a new StreamWriter.

TextWriter textWriter = new StreamWriter("../../Box.xml");

这归结为:

  • ../ - 转到上一级目录
  • ../ - 转到上一级目录
  • Box.xml文件在这里

所以,当你想要一个文件夹中创建的根文件夹,你可以使用里面的文件:

So when you want the file created in a folder inside the root folder you could use:

  • ../ 文件夹名称 /Box.xml
  • "../foldername/Box.xml"

但是,如果你不希望依赖于当前的文件位置,你也可以使用:

But if you don't want to rely on your current file location you can also use:

AppDomain.CurrentDomain.BaseDirectory

这将使:

var path = String.Format("{0}foldername\Box.xml", AppDomain.CurrentDomain.BaseDirectory); TextWriter textWriter = new StreamWriter(path);

希望这有助于。

Hope this helps.

更多推荐

将文件保存在项目中的指定文件夹中

本文发布于:2023-11-12 12:01:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581447.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   项目   夹中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!