简体   繁体   English

在针对已编码的ui的调试中运行代码时,如何获取文件的相对路径

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

相关问题 我如何在设置中使用字符串文件路径,以便我可以在我的代码中使用该路径 - How do i use a string file path in settings so i can get that path useable in my code 如何在asp .net / c#项目代码中使用脚本文件夹的相对路径? - how do i use the relative path to the scripts folder in my asp .net / c# project code? 如何获取父目录的路径作为相对路径 - How do I get the path to the parent directory as relative path 如何在运行Coded UI时解析当前目录 - How to resolve Current Directory when running Coded UI 如何使用相对路径引用WSDL文件? - How do I reference a WSDL file with a relative path? 如何使用编码的UI测试获取Wpf DataGrid的全部内容 - How do I get the entire contents of a Wpf DataGrid with Coded UI Test 如何使用相对路径指向我希望将数据导入数据模型的文件? - How do I use a relative path to point to a file from which I wish to import data to my Data Model? 如何在后台代码运行C#时使UI不冻结 - How do I make my UI not Freeze while background code is running C# 运行编码的UI测试时控制台未出现 - Console not appearing when running Coded UI test 如何将SQL Server数据库与编码的UI测试一起使用? - How do I use a SQL Server database with Coded UI Test?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM