为什么System.AppDomain.CurrentDomain.BaseDirectory返回不同的结果? [英] Why does System.AppDomain.CurrentDomain.BaseDirectory return different results?

查看:483
本文介绍了为什么System.AppDomain.CurrentDomain.BaseDirectory返回不同的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将数据库的路径(带有一些xml文件的文件夹)存储在app.config中.在启动时,我检查路径是否存在.如果不存在,我想将路径设置为默认路径.代码如下:

I store the path of my database (a folder with some xml files) in the app.config. At the startup I check, if the path exists. If it doesn't exist, I want to set the path to the default path. Code looks like this:

public void CheckAndRepairSettings()
{
        /* Check Paths */
        if(GetDatabasePath() == null)
             SetDatabasePath(System.AppDomain.CurrentDomain.BaseDirectory + "DataBase");
}

GetDatabasePath()从app.config读取路径,而SetDatabasePath()将路径写入app.config.这些方法工作正常.

GetDatabasePath() reads the path form the app.config and SetDatabasePath() writes the path to the app.config. These Methods are working fine.

我的问题是System.AppDomain.CurrentDomain.BaseDirectory.如果我在应用程序调试模式下运行此命令,则会得到: "F:\ Office \ Projekte_Software \ ServiceTool \ _Work \ ServiceSoftware \ ServiceSoftware \ bin \ Debug \"

My promblem is the System.AppDomain.CurrentDomain.BaseDirectory. If I run this in my applications debug mode I get: "F:\Office\Projekte_Software\ServiceTool\_Work\ServiceSoftware\ServiceSoftware\bin\Debug\"

我还使用NUnit进行某些单元测试.如果我以调试模式运行NUnit,则会得到: "F:\ Office \ Projekte_Software \ ServiceTool \ _Work \ ServiceSoftware \ ServiceSoftware.UnitTests \ bin \ Debug"

I additionally use NUnit for some unit tests. If I run NUnit in debug mode I get : "F:\Office\Projekte_Software\ServiceTool\_Work\ServiceSoftware\ServiceSoftware.UnitTests\bin\Debug"

在NUnit调试模式下,路径中没有结尾的反斜杠"\",因此在连接CheckAndRepairSettings()中的路径字符串时,我得到的路径不存在.

There is no trailing Backslash "\" in the path in NUnit debug mode, so I get a non existing path when I concatenate the path-string in my CheckAndRepairSettings().

为什么这种行为如此不同?

Why does this behave so different?

推荐答案

您应该使用Path.Combine来连接路径,它可以处理与现有/不存在的路径分隔符有关的问题

You should use Path.Combine to concatenate paths, it handles issues regarding existing/non-existing (among other things) path separators

为什么其中一个包含斜杠而另一个不包含斜杠,可能与nUnit如何创建在其下运行测试的appdomain有关

Why one includes ending slash the other one doesn't is probably related to how nUnit creates the appdomain under which it runs its tests

这篇关于为什么System.AppDomain.CurrentDomain.BaseDirectory返回不同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆