如何读取来自控制台应用程序的 web.config ?
我有 VB.net 控制台应用程序。我想从 web.config 读取 (ConnectionString)。
Web.config
位于我的虚拟 PC 中的特定路径,例如 "C:/mywebConfig"
<add name="MY_DB" connectionString="Data Source=DATASOURCE;Initial Catalog=DB;Persist
Security Info=False; User ID=***;Password=****;" providerName="System.Data.SqlClient" />
我的代码:
Dim connString As String = String.Empty
connString = ConfigurationManager.ConnectionStrings("MY_DB").ConnectionString
每当我尝试访问它时,我都会收到错误消息设置为对象的实例或类似的东西:)
请帮忙。
我尝试在我的项目中添加 web.config,但仍然收到错误。
I have VB.net console application. I would like to read the (ConnectionString) from a web.config.
Web.config
is located at a particular path in my virtual PC, say "C:/mywebConfig"
<add name="MY_DB" connectionString="Data Source=DATASOURCE;Initial Catalog=DB;Persist
Security Info=False; User ID=***;Password=****;" providerName="System.Data.SqlClient" />
My code:
Dim connString As String = String.Empty
connString = ConfigurationManager.ConnectionStrings("MY_DB").ConnectionString
Whenever I try to access it, i get the error not set to an instance of an object or something like that :)
Help please.
I tried to add the web.config in my Project, but still get the error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您确实想阅读另一个(网络)应用程序的
app.config
或web.config
,请查看 ConfigurationManager.OpenMappedExeConfiguration。If you do indeed want to read the
app.config
orweb.config
of another (web) app, take a look at ConfigurationManager.OpenMappedExeConfiguration.正如 Matt 建议的控制台应用程序,您需要包含一个 app.config 文件。为了轻松阅读该文件,您可以引用 System.Configuration 程序集,然后使用 System.Configuration.ConfigurationManager.ConnectionStrings 属性。
例如:
编辑
我刚刚重新阅读了您的帖子,检查您的应用是否有权从 c:\ 驱动器读取 .config 文件可能是个好主意。
As Matt suggested for a console app you need to include a app.config file instead. For easy reading of that file you can reference the System.Configuration assembly and then use the System.Configuration.ConfigurationManager.ConnectionStrings property.
For example:
EDIT
I've just re-read your post and it might be a good idea to check your app has permission to read .config files from the c:\ drive.
web.config
用于 Web 应用程序 - 您将需要一个app.config
文件。查看此链接以了解差异:
dotNET - app.config 和 web.config< /a>
还有这个堆栈溢出问题app.config 的用途
web.config
is used for web applications - you will need aapp.config
file.Have a look at this link for the differences:
dotNET - app.config and web.config
Also this stack overflow question what is app.config for