为什么Windows Forms和图书馆之间AppDomain.CurrentDomain.BaseDirectory不同图书馆、不同、Forms、Windows

2023-09-04 00:57:27 作者:做我的宝贝叭

在我的WinForms应用程序,AppDomain.CurrentDomain.BaseDirectory设置为C:\项目\ TestProject的\斌\调试\

In my winforms application, AppDomain.CurrentDomain.BaseDirectory is set to "C:\Projects\TestProject\bin\Debug\"

在我的单元测试是C:\项目\ TestProject的\斌\调试(没有最后的斜杠)。这是为什么?

In my unit tests it is "C:\Projects\TestProject\bin\Debug" (no final slash). Why is this?

// @ 会的?我问为什么测试项目的目录没有斜杠

@Will : I am asking why the test project's directory doesn't have a trailing slash?

推荐答案

您也许会问两个可能一个问题:为什么他们不同的,为什么测试项目的目录没有斜杠

You may be asking one of two possible questions: Why are they different, or why the test project's directory doesn't have a trailing slash.

假设它的第一个:这就是在code都执行。当您调试程序,它的编译和二进制文件都放在项目的\ BIN \ debug目录下。当你进行测试时,你正在运行测试的二进制文件,这是编译和测试项目的bin \ Debug目录之下。

Assuming its the first: That's where the code is executing from. When you debug the program, its compiled and the binaries are placed under the project's \bin\debug directory. When you're testing, you're running the test's binaries, which are compiled and placed under the test project's bin\debug directory.

假设它的最后一个。可能是一些模糊的原因,可能是一个错误,或可能赶上的人谁是串联的路径,而不是使用Path.Combine(!淘气)

Assuming its the last: Possibly some obscure reason, possibly a bug, or possibly to catch people who are concatenating paths rather than using Path.Combine (naughty naughty!).

好了,我不知道为什么它的不同。测试应用程序可能会在自定义CLR主机上运行;我想这可能是这样的测试应用程序做一些奇怪的东西与通常不属于标准的CLR宿主内允许私人访问。我只是抓住了救命稻草这里我没有怎么这个东西实际上是被codeD任何实际的知识。

Well, I don't know why it's different. Test applications may be run within a custom CLR host; I think this may be the case as test apps do some weird stuff with private accessors that normally aren't allowed within the standard CLR host. I'm only grasping at straws here as I don't have any actual knowledge about how this stuff is actually being coded.

总之,解决办法站(Path.Combine)。没有人应串联路径,路径定界符可以改变。

Anyhow, the workaround stands (Path.Combine). Nobody should be concatenating paths, as path delimeters can change.