将文件保存在项目中的指定文件夹中 [英] Saving a file in a specified folder inside my project
问题描述
我创建一个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.
这篇关于将文件保存在项目中的指定文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!