[英]How to resolve Current Directory when running Coded UI
I am running a CodedUI test that references a DLL and that DLL refrences a sort of "config" file. 我正在运行一个引用DLL的CodedUI测试,该DLL引用了一种“配置”文件。 While running the test the current Directory returns the one where CodedUI puts the test results files I've used 在运行测试时,当前Directory返回CodedUI放置我使用过的测试结果文件的目录
AppDomain.CurrentDomain.BaseDirectory
and 和
System.Reflection.Assembly.GetExecutingAssembly().CodeBase
and 和
System.Reflection.Assembly.GetExecutingAssembly().Location
These all give me the same path 这些都给了我相同的道路
What I need is to get the path where the DLL resides because that is where the config file will be built. 我需要的是获取DLL所在的路径,因为这是构建配置文件的位置。
The location where this will be changes if I am debugging or if I am just running the test (obviously) so I can't use that and navigate backwards or anything like that. 如果我正在调试或者我刚刚运行测试(显然),这将改变的位置,所以我不能使用它并向后导航或类似的东西。
Are there any other ways to get the location of the DLL you are referencing?? 有没有其他方法来获取您引用的DLL的位置?
Edit: 编辑:
I am referencing this config file from inside the DLL that i am referencing. 我从我引用的DLL内部引用此配置文件。
So far the only place I have found the original path to the test dll is in a private variable in the test context. 到目前为止,我找到测试dll的原始路径的唯一地方是测试上下文中的私有变量。 I ended up using reflection to get the value out and make it usable. 我最终使用反射来获取值并使其可用。
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
public static string CodeBase(
TestContext testContext)
{
System.Type t = testContext.GetType();
FieldInfo field = t.GetField("m_test", BindingFlags.NonPublic | BindingFlags.Instance);
object fieldValue = field.GetValue(testContext);
t = fieldValue.GetType();
PropertyInfo property = fieldValue.GetType().GetProperty("CodeBase");
return (string)property.GetValue(fieldValue, null);
}
I used this to get the path to the DLL that is getting run and use that to then run the application that I know is compiled to the same location as the test was at. 我使用它来获取正在运行的DLL的路径,然后使用它来运行我知道编译到与测试所在位置相同的位置的应用程序。
If anyone finds a better way of getting this, please let me know as well. 如果有人找到更好的方法,请告诉我。
The best way to get the directory of where a given DLL was loaded from is to use the following on a type that is defined in that assembly. 获取加载给定DLL的目录的最佳方法是在该程序集中定义的类型上使用以下内容。
var type = typeof(TypeInThatAssembly);
var path = Path.GetDirectory(type.Location);
The CodeBase
and Location
property often return the same information but are very different CodeBase
和Location
属性通常返回相同的信息,但是非常不同
These can differ in application which use shadow copied assemblies (Asp.Net, xUnit, etc ...) 这些可以在使用阴影复制程序集(Asp.Net,xUnit等)的应用程序中有所不同
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.