我正在使用字符串执行我的执行程序集路径,我不希望我需要使用其中一部分的整个路径,并且需要从该路径访问另一个文件夹。
假设我需要从文件夹中获取xml
" E:\Mahi_WorkSpace\TFS \Atrias.WebAutomationTesting\XmlFolder"
我的执行程序集路径是
" E:\Mahi_WorkSpace\TFS \Atrias.WebAutomationTesting\TestResults\t.mahidharreddy_123 2016-10-24 07_39_26\Out"
现在我只需要执行程序集字符串
中的"E:\Mahi_WorkSpace\TFS \Atrias.WebAutomationTesting\ "
public string GetStringBody()
{
System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
string path=Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
//srting xmlpath=??
return xmlpath;
}
答案 0 :(得分:1)
试试这个。
string path = Path.GetDirectoryName(Path.GetDirectoryName(TestContext.TestRunDirectory));
这将为您提供由TestResults
创建的Atrias.WebAutomationTesting
文件夹的父目录,我相信这是您正在寻找的内容。
答案 1 :(得分:0)
您可以遍历父目录,直到到达目标节点。
var currentDirectoryPath = AppDomain.CurrentDomain.BaseDirectory;
var parentDirectory = System.IO.Directory.GetParent(currentDirectoryPath);
while (!string.Equals(parentDirectory.Name, "Atrias.WebAutomationTesting", StringComparison.CurrentCultureIgnoreCase))
{
parentDirectory = parentDirectory.Parent;
if (parentDirectory == null)
break;
}
if (parentDirectory != null)
{
var targetDirectory = parentDirectory.FullName + "\\Test";
MessageBox.Show(targetDirectory);
}
答案 2 :(得分:0)
这只是另一种可能的解决方案:
public string GetStringBody() {
System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
var di = new DirectoryInfo(path);
return di.Parent.Parent.Parent.FullName; // here you should define how many level you want to up
}