[英]Getting current path in Windows form application not working properly
I'm working on an app linked to a local db and I'd like to get the path of db d y namically. 我正在链接到本地数据库的应用程序,我想获得分贝d y的路径namically。 I don't know where exactly users will copy this app, so I'd like to get the path to the db automatically. 我不知道用户将在哪里精确复制此应用程序,因此我想自动获取数据库的路径。 I wrote the following code, it gets the path, but not exactly. 我编写了以下代码,它获取了路径,但不完全是。 I mean, if I have my app in: C:\\Users\\ROG\\Desktop
, it says that there is no db in C:\\Users\\ROG
. 我的意思是,如果我的应用程序位于: C:\\Users\\ROG\\Desktop
,则表示C:\\Users\\ROG
没有数据库。 So it doesn't get the last location of it. 因此它没有得到它的最后位置。 Why is that and how to solve it? 为什么会这样以及如何解决呢?
I connect to it as follow: 我将其连接如下:
var connString = (@"Data Source=" +
Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName +
@"\Angajati.sdf");
try to use the following code 尝试使用以下代码
var connString = (@"Data Source=" +
System.IO.Path.Combine(System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().Location),
"Angajati.sdf"));
Rather than using get current directory I suggest using Application Path. 建议不要使用“获取当前目录”,而应使用“应用程序路径”。 That's because if an OpenFileDialog
be used the current directory changes but the applicationPath is the same. 这是因为如果使用OpenFileDialog
则当前目录会更改,但是applicationPath是相同的。
System.Reflection.Assembly.GetExecutingAssembly().Location
I hope it would be helpful. 希望对您有所帮助。
Consider using the following code to determine appllication base directory: 考虑使用以下代码来确定应用基本目录:
AppDomain.CurrentDomain.BaseDirectory
Full code will look like the following: 完整的代码如下所示:
var connString = "Data Source=" +
System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Angajati.sdf");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.