简体   繁体   中英

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. 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"

If possible I'd like to point it to "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. 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

The TestContext class contains several fields with "directory" in their names. These can be used to access the various directories associated with running the tests. See also https://stackoverflow.com/a/19682311/546871

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM