[英]How do i get a relative path of a file when running my code in debug for coded ui
I'm trying not to hard code my path, but I have not been able to figure our a way to get to an xml file that I have included in my project under a folder labeled Datasource. 我尝试不对路径进行硬编码,但无法找到一种方法来获取我的项目中包含的xml文件,该文件位于数据源文件夹下。 Here is my latest code that I have tried which still doesn't work.
这是我尝试过的最新代码,仍然无法使用。
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);
Here is the output for the path that I'm getting which is putting it in my test results output folder. 这是我要获取的路径的输出,将其放在测试结果输出文件夹中。
"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 \\ TestResults \\ marcw_ISD2005M 2016-02-05 10_15_17 \\ Out \\ DataSource \\ Miser_Login_Dts.xml”
If possible I'd like to point it to "C:\\MyAutomation\\Automated_Test_Projects\\AutomationProjects\\MiserReleaseTestSuites\\MiserReleaseTestSuites\\DataSource\\Miser_Login_DTs.xml" 如果可能的话,我想将其指向“ C:\\ MyAutomation \\ Automated_Test_Projects \\ AutomationProjects \\ MiserReleaseTestSuites \\ MiserReleaseTestSuites \\ DataSource \\ Miser_Login_DTs.xml”
".."
Can be used to go to the relative parent directory. ".."
可用于转到相对父目录。 "."
Refers to the current directory. 引用当前目录。
You can combine these to form a relative path that starts higher up in the directory tree. 您可以将它们组合起来,形成一个相对路径,该相对路径从目录树的上方开始。
In your example you need to go 3 directories higher than the out
folder and then into the MiserReleaseTestSuites\\DataSource
folder. 在您的示例中,您需要比
out
文件夹高3个目录,然后进入MiserReleaseTestSuites\\DataSource
文件夹。 Combining this produces 结合产生
@"..\\..\\..\\MiserReleaseTestSuites\\DataSource\\"
You can deploy the file in the same manner as you would when data driving the tests. 您可以采用与数据驱动测试时相同的方式来部署文件。 See https://stackoverflow.com/a/25742114/546871
参见https://stackoverflow.com/a/25742114/546871
The TestContext
class contains several fields with "directory" in their names. TestContext
类包含几个名称中带有“目录”的字段。 These can be used to access the various directories associated with running the tests. 这些可用于访问与运行测试相关的各种目录。 See also https://stackoverflow.com/a/19682311/546871
另请参阅https://stackoverflow.com/a/19682311/546871
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.