![](/img/trans.png)
[英]How to combine paths by preserving the original path's directory separator in C#?
[英]How to open directory few levels down of original path C#?
我需要从执行应用程序的位置向下三级打开一个文件夹(原始示例存在一些缺陷):
// 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
来简化和更可靠的组合。
更改Magazines.accdb的属性,以便在构建项目时将其复制到bin \\ Debug文件夹。
您可以通过在解决方案资源管理器中右键单击文件,然后将“复制到输出目录”更改为“始终复制”来执行此操作。
几个选项只是将数据库与可执行文件放在同一文件夹中(因此,在调试文件夹中),这实际上是有道理的,因为当您将可执行文件打包到某个人时,通常会得到结果文件夹结构。
另一个选择是,如果数据库文件已添加到项目中,则可以将“复制到输出目录”指定为“如果更新则复制”,如果更新则将复制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.