2010-07-20 110 views
0

我正在創建一個簡單的.NET控制檯應用程序,我想將文件保存在作爲根項目一部分的文件夾中,如下所示:SolutionName.ProjectName\TestData\。我想將test.xml放入TestData文件夾中。但是,當我去保存我的XDocument時,它將它保存到SolutionName.ProjectName\bin\x86\Debug Console\test.xml保存/檢索項目目錄中的文件,而不是輸出目錄

我需要做什麼來保存或檢索項目目錄的子文件夾中的文件?

回答

2

您的控制檯應用程序一經編譯就不再與您的Visual Studio解決方案真正相關。最好的辦法可能就是簡單的「養活」的輸出路徑到你的應用程序作爲命令行參數:

Public Sub Main(args as String()) 

    ' don't forget validation: handle situation where no or invalid arguments supplied 

    Dim outputFile = Path.Combine(args(0), "TestData", "test.xml") 

End Sub 

現在你可以像這樣運行你的應用程序:

myapp.exe Path\To\SolutionFolder 
0

您可以使用System.IO命名空間到目錄的相對你得到你的EXE:

var exeDirectory = Application.StartupPath; 
var exeDirectoryInfo = new DirectoryInfo(exeDirectory); 
var projectDirectoryInfo = exeDirectoryInfo.Parent.Parent.Parnet; // bin/x86/debug to project 

var dataPath = Path.Combine(projectDirectoryInfo.FullName, "TestData"); 

var finalFilename = Path.Combine(dataPath, "test.xml"); 
+0

我想今天是路線,我喜歡這個建議,但是我找不到'Application.StartupPath'。我如何在控制檯應用程序中找到(或類似)? – 2010-07-20 17:34:14

2

我以前見過這一點,但實際上從未嘗試過了。做了一個快速搜索和挖起來:

System.AppDomain.CurrentDomain.BaseDirectory 

給你一個鏡頭,而不是Application.StartupPath爲您的控制檯應用程序。

+0

這也進入bin/debug – shahensha 2012-03-13 12:44:56

相關問題