app.config 访问运行时异常
我正在处理一个用 C# 编写的 .NET Windows 窗体应用程序,使用实体框架进行数据库连接。连接字符串写入app.config中。重点是,我修改应用程序以便在两个不同的数据库之间切换:SQL Server Express 2008 和 SQL Compact 4。如果应用程序无法连接到 SQL Server,它会修改 app.config 连接字符串部分来解析名称以便连接本地数据库(SQL紧凑4)。问题是,在某些计算机中,操作会引发异常:对路径的访问被拒绝,其中路径是安装应用程序的路径。我怎样才能克服这个问题呢?
I'm dealing with a .NET windows form application written in C#, using entity framework for database connection. The connection string is written in app.config. The point is that I modify the application in order to switch between two different databases: a SQL server express 2008 and a SQL compact 4. In case the application cannot connect to SQL server, it modifies the app.config connection string section to resolve a name in order to connect with the local database (SQL compact 4). The problem is that in some computers is operation is firing an exception: access to the path denied, where the path is the one where the application is installed. How could I possibly overcome this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
用户模式应用程序不应尝试写入磁盘中除用户配置文件之外的任何位置。这意味着
c:\Program Files
中的任何内容都应该禁止该程序访问,除非它是管理程序(因此在 Win7/Vista 中需要提升权限)。以管理员身份运行的 Windows XP 系统可能工作正常;但 Vista/7 系统可能会遇到异常,甚至是无提示的编辑失败。 (由于被重定向到 app.config 的用户特定副本,而不是真实的副本)。
您应该将各种连接字符串存储在 app.config 中,并为用户提供一种在应用程序中从中进行选择的方法。您可以使用用户设置来保存最后使用的字符串。
A user-mode application should not attempt to write to disk anywhere except in the user's profile. That means that anything in
c:\Program Files
should be off-limits to the program, unless it is an administrative program (and therefore would demand elevation in Win7/Vista).Windows XP systems with users running as admin probably work fine; But Vista/7 systems might experience exceptions, or even silently-failing edits. (due to being redirected to a user-specific copy of the app.config, instead of the real one).
You should instead store the various connection strings in the app.config, and provide a way for the user to select from them in your application. You can use user settings to save which string was used last.