[英]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.