[英]How do i use a string file path in settings so i can get that path useable in my code
[英]How do i get a relative path of a file when running my code in debug for coded ui
我嘗試不對路徑進行硬編碼,但無法找到一種方法來獲取我的項目中包含的xml文件,該文件位於數據源文件夾下。 這是我嘗試過的最新代碼,仍然無法使用。
public static string myAssemblyDirectory
{
get
{
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}
string fileName = xmlFileName;
string path = Path.Combine(myAssemblyDirectory, @"DataSource\" + fileName);
XmlDocument xDoc = new XmlDocument();
xDoc.Load(path);
這是我要獲取的路徑的輸出,將其放在測試結果輸出文件夾中。
“ C:\\ MyAutomation \\ Automated_Test_Projects \\ AutomationProjects \\ MiserReleaseTestSuites \\ TestResults \\ marcw_ISD2005M 2016-02-05 10_15_17 \\ Out \\ DataSource \\ Miser_Login_Dts.xml”
如果可能的話,我想將其指向“ C:\\ MyAutomation \\ Automated_Test_Projects \\ AutomationProjects \\ MiserReleaseTestSuites \\ MiserReleaseTestSuites \\ DataSource \\ Miser_Login_DTs.xml”
".."
可用於轉到相對父目錄。 "."
引用當前目錄。
您可以將它們組合起來,形成一個相對路徑,該相對路徑從目錄樹的上方開始。
在您的示例中,您需要比out
文件夾高3個目錄,然后進入MiserReleaseTestSuites\\DataSource
文件夾。 結合產生
@"..\\..\\..\\MiserReleaseTestSuites\\DataSource\\"
您可以采用與數據驅動測試時相同的方式來部署文件。 參見https://stackoverflow.com/a/25742114/546871
TestContext
類包含幾個名稱中帶有“目錄”的字段。 這些可用於訪問與運行測試相關的各種目錄。 另請參閱https://stackoverflow.com/a/19682311/546871
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.