如何打开目录下几层的原始路径c#
本文关键字:几层 原始 路径 何打开 | 更新日期: 2023-09-27 18:14:05
我需要从应用程序执行的地方打开一个文件夹3层(原始示例我有一些缺陷):
// find the path where the executable resides
string dbPath = Application.StartupPath;
// constructing the connection string - double slashes
string connString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="
+ dbPath + "''..''..''..''Magazines.accdb; User Id=admin; Password=";
但是这将打开:
C:'Documents and Settings'Server'Desktop'Lab 10'Lab 10'Lab 10'bin'Debug'..'..'..'Magazines.accdb
程序开始的原始目录:
C:'Documents and Settings'Server'Desktop'Lab 10'Lab 10'Lab 10'bin'Debug'
我需要它是:
C:'Documents and Settings'Server'Desktop'Lab 10'Lab 10'Magazines.accdb
这样做的正当理由是什么?
使用DirectoryInfo来计算'..'
var path = new DirectoryInfo (Path.Combine( "c:/bla", "../newBla")).FullName()
也可以使用Path.Combine
,以获得更轻松可靠的组合。
改变杂志的属性。accdb,以便在构建项目时将其复制到bin'Debug文件夹。
您可以在解决方案资源管理器中右键单击文件,然后将复制到输出目录更改为始终复制。
两个选项是将数据库放在与可执行文件相同的文件夹中(因此,在您的调试文件夹中),这实际上是有意义的,因为当您将可执行文件打包给某人时,您通常会得到结果文件夹结构。
另一个选项是,如果数据库文件被添加到项目中,你可以将"Copy to Output Directory"指定为"Copy if Newer",这将复制如果是新的