C# WebConfig.ConnectionString 中的相对路径引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/125157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Relative path reference in WebConfig.ConnectionString
提问by Graviton
Is it possible to specify a relative path reference in connectionstring, attachDbFileName property in a web.config?
是否可以在 web.config 中的 connectionstring、attachDbFileName 属性中指定相对路径引用?
For example, In my database is located in the App_data folder, I can easily specify the AttachDBFilename as|DataDirectory|\mydb.mdf and the |Datadirectory| will automatically resolve to the correct path.
例如,在我的数据库位于 App_data 文件夹中,我可以轻松地将 AttachDBFilename 指定为|DataDirectory|\mydb.mdf 和 |Datadirectory|。会自动解析到正确的路径。
Now, suppose that web.config file is located in A folder, but the database is located in B\App_data folder, where A and B folder is located in the same folder. Is there anyway to use relative path reference to resolve to the correct path?
现在,假设 web.config 文件位于 A 文件夹中,但数据库位于 B\App_data 文件夹中,其中 A 和 B 文件夹位于同一文件夹中。无论如何使用相对路径引用来解析正确的路径?
回答by Jared
It depends on where your '|DataDirectory|' is located. If the resolved value of '|DataDirectory|' is in folder A (where the web.config is), then no - you can't specify a relative path that is not a subfolder of the resolved value of '|DataDirectory|'.
这取决于您的“|DataDirectory|”在哪里 位于。如果'|DataDirectory|'的解析值 位于文件夹 A(web.config 所在的位置)中,则不 - 您不能指定不是“|DataDirectory|”解析值的子文件夹的相对路径。
What you cando is set the value of '|DataDirectory|' to be wherever you would like, by calling the AppDomain.SetDatamethod.
您可以做的是设置“|DataDirectory|”的值 通过调用AppDomain.SetData方法,您可以随心所欲。
From the MSDN online documentation:
来自 MSDN 在线文档:
When DataDirectory is used, the resulting file path cannot be higher in the directory structure than the directory pointed to by the substitution string. For example, if the fully expanded DataDirectory is C:\AppDirectory\app_data, then the sample connection string shown above works because it is below c:\AppDirectory. However, attempting to specify DataDirectory as |DataDirectory|..\data will result in an error because \data is not a subdirectory of \AppDirectory.
使用 DataDirectory 时,生成的文件路径在目录结构中不能高于替换字符串指向的目录。例如,如果完全展开的 DataDirectory 是 C:\AppDirectory\app_data,那么上面显示的示例连接字符串有效,因为它在 c:\AppDirectory 之下。但是,尝试将 DataDirectory 指定为 |DataDirectory|..\data 将导致错误,因为 \data 不是 \AppDirectory 的子目录。
Hope this helps.
希望这可以帮助。
回答by CodeRedick
In IIS you could also create a virtual directory that points at wherever the the real database is kept. Then your connection string just references the virtual directory.
在 IIS 中,您还可以创建一个虚拟目录,指向保存真实数据库的位置。然后您的连接字符串只引用虚拟目录。
回答by jbandi
I had the same problem with the following scenario: I wanted to use the same database as the application from my integration tests.
我在以下场景中遇到了同样的问题:我想使用与集成测试中的应用程序相同的数据库。
I went with the following workaround:
我采用了以下解决方法:
In the App.config of my test-project I have:
在我的测试项目的 App.config 中,我有:
<appSettings>
<add key="DataDirectory" value="..\..\..\BookShop\App_Data\"/>
</appSettings>
In the test-setup I execute the following code:
在测试设置中,我执行以下代码:
var dataDirectory = ConfigurationManager.AppSettings["DataDirectory"];
var absoluteDataDirectory = Path.GetFullPath(dataDirectory);
AppDomain.CurrentDomain.SetData("DataDirectory", absoluteDataDirectory);
回答by JohnSpin
Add the following attributes to the test method:
将以下属性添加到测试方法:
[DeploymentItem("..\TestSolutionDir\TestProjedtDir\TestDataFolder\TestAutomationSpreadsheet.xlsx")]
[DataSource("System.Data.Odbc", "Dsn=Excel Files;dbq=|DataDirectory|\TestAutomationSpreadsheet.xlsx", "SpreadsheetTabName$", DataAccessMethod.Sequential)]
The |DataDirctory|
variable is defined by the system when it runs the test. The DeploymentItem copies the spreadsheet there. You point to the spreadsheet and to the tab within the spreadsheet that the data is coming from. Right-click on the tab to rename it to something easy to remember.
该|DataDirctory|
变量由系统在运行测试时定义。DeploymentItem 将电子表格复制到那里。您指向电子表格和电子表格中数据来源的选项卡。右键单击选项卡将其重命名为易于记忆的名称。
回答by Unni Krishnan SJ Nair
Web.config
网页配置
<appSettings>
<add key="FilePath" value="App_Data\SavedFiles\"/>
</appSettings>
Path.cs
路径文件
string filePath = AppDomain.CurrentDomain.BaseDirectory + (ConfigurationManager.AppSettings["FilePath"]);
Works for me..!!
对我有用..!!