繁体   English   中英

在Windows窗体应用程序中获取当前路径无法正常工作

[英]Getting current path in Windows form application not working properly

我正在链接到本地数据库的应用程序,我想获得分贝d y的路径namically。 我不知道用户将在哪里精确复制此应用程序,因此我想自动获取数据库的路径。 我编写了以下代码,它获取了路径,但不完全是。 我的意思是,如果我的应用程序位于: C:\\Users\\ROG\\Desktop ,则表示C:\\Users\\ROG没有数据库。 因此它没有得到它的最后位置。 为什么会这样以及如何解决呢?

我将其连接如下:

var connString = (@"Data Source=" + 
                  Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + 
                  @"\Angajati.sdf");

尝试使用以下代码

var connString = (@"Data Source=" + 
                  System.IO.Path.Combine(System.IO.Path.GetDirectoryName(
                         System.Reflection.Assembly.GetExecutingAssembly().Location),
                  "Angajati.sdf"));

建议不要使用“获取当前目录”,而应使用“应用程序路径”。 这是因为如果使用OpenFileDialog则当前目录会更改,但是applicationPath是相同的。

System.Reflection.Assembly.GetExecutingAssembly().Location

希望对您有所帮助。

考虑使用以下代码来确定应用基本目录:

AppDomain.CurrentDomain.BaseDirectory

完整的代码如下所示:

var connString = "Data Source=" + 
              System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Angajati.sdf");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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