简体   繁体   English

用于在特定文件夹C#中查找特定文件的自定义路径

[英]Custom Path for locating a specific file in specific folder c#

I want to look up at the database's file in the specific folder and what I did before is like this: 我想查看特定文件夹中的数据库文件,而我之前所做的是这样的:

"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:/Fuhans's Folder/Project/SIA - Point of Sales/SIA - Point of Sales/Assets/Database/db1.accdb; Persist Security Info=False;";

and I am thinking that is there a shortcut way to look up the database's file, instead of writing a specific folder? 并且我认为这是查找数据库文件而不是编写特定文件夹的快捷方式吗?

and I came up with: 我想出了:

"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=../Assets/Database/db1.accdb; Persist Security Info=False;";

But, when I changed it into like the second one, the error message appear like below: 但是,当我将其更改为第二种时,错误消息如下所示:

在此处输入图片说明

My question is: Is there a way to delete the bin location path, but still doing the look up the database's file like the second one? 我的问题是:有没有办法删除bin位置路径,但是仍然像第二个一样查找数据库文件?

Thank you 谢谢

在相对路径前面添加另一个../

"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=../../Assets/Database/db1.accdb; Persist Security Info=False;";

You should avoid to use absolute paths in your connection string and relative paths (with .. are pretty easy to be broken when you deploy). 您应该避免在连接字符串和相对路径中使用绝对路径( ..在部署时很容易被破坏)。

In ASP.NET to use a relative path you may use |DataDirectory|db1.accdb , in WinForms it's not available but that constants are resolved using Application Domain properties. 在ASP.NET中使用相对路径,您可以使用|DataDirectory|db1.accdb ,在WinForms中它不可用,但常数是使用Application Domain属性解析的。 Simply set your base bath inside your main() method: 只需在您的main()方法中设置基础浴即可:

AppDomain.Current.SetData("RootFolder",
    @"D:\Fuhans's Folder\Project\SIA - Point of Sales\SIA - Point of Sales");

And your connection string: 和您的连接字符串:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|RootFolder|Assets/Database/db1.accdb;

Why you should use an absolute path instead of a relative one? 为什么要使用绝对路径而不是相对路径? Because it won't be affected by working directory and it's easier to add multiple constants: 因为它不受工作目录的影响,并且添加多个常量更容易:

AppDomain.Current.SetData("Bin",
    Application.StartupPath);
AppDomain.Current.SetData("AppData",
    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));

Second option will work much better with UAC (where program's folder may not be writeable at all). 第二个选项将与UAC更好地工作(其中程序的文件夹可能根本无法写入)。 Also note that path is still navigable because it'll be normalized. 还要注意,路径仍然可以导航,因为它将被规范化。

Assuming your compiling to bin/debug in your development environment: 假设您要在开发环境中进行bin / debug编译:

Data Source=|Bin|../../Assets/Database/db1.accdb

Assuming your application will be deployed and it'll work on data folder (not program folder): 假设您的应用程序将被部署并且将在数据文件夹(而不是程序文件夹)上运行:

Data Source=|AppData|Assets/Database/db1.accdb

Straight and easy. 直而容易。

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

相关问题 C#可以按路径打开特定文件的容器文件夹吗? - Can C# open a the container folder of a specific file by its path? c#将pdf文件保存到特定路径 - c# save pdf file to specific path 如何将文件从特定文件夹复制到C#中的共享文件夹? - How to copy file from a specific folder to a shared folder in C#? 有没有办法动态定位我当前 c# Winforms 应用程序的子文件夹/子文件的路径更改? - Is there a way of dynamically locating a change of path of a sub-folder/sub-file of my current c# Winforms application? C#SaveFileDialog在特定文件夹中 - C# SaveFileDialog in specific folder 使用SaveFileDialog将文件保存到c#中的特定文件夹 - Save File to specific folder in c#, using SaveFileDialog 将文件保存到 Windows Form C# 中的特定文件夹 - Save file to a specific folder in Windows Form C# 如何以编程方式将任何文件保存在C#中的特定文件夹中 - how to save any file in specific folder in c# programatically MSGraph/GraphServiceClient OneDrive 使用 C# 将文件上传到特定站点和/或文件夹 - MSGraph/GraphServiceClient OneDrive uploading file to specific Site and/or Folder with C# C#:Firefox Webdriver:如何将文件下载到特定文件夹 - C#: Firefox webdriver: How to download file to a specific folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM