有没有办法在应用程序设置完成后提示用户设置一次路径?
我在C#.NET中完成了一个项目,我的数据库文件是Excel工作簿。由于连接字符串的位置在我的编码中是硬编码的,因此在我的系统中安装它没有问题。对于其他系统,位置将不匹配..有没有办法在设置后最初设置路径,以便所有用户可以在不同的系统中使用它?
答案 0 :(得分:0)
只需在程序开始时询问。如果用户提供值或选择“默认”,则设置注册表或临时文件或其他一些标志,如果需要,请在下次使用此提醒。
using System.IO;
.
.
... Main...
if(!File.Exists("user_has_toldme_already")){
... ask the user
File.WriteAllText("user_has_toldme_already","dummy text");
.
. Set the value of my connectionstring (you could even record it in the file above
. and load it when you need it
}
.
. rest of code
.
答案 1 :(得分:0)
您需要将配置文件添加到项目中。
Codelicious解释了如何做到这一点。
为该文件添加一些设置,例如
<appSettings>
<add key="OperatorName" value="Rita"></add>
<add key="LoggerLevel" value="5"></add>
</appSettings>
使用ConfigurationManager
类访问设置。
此网站 - ConnectionStrings.com - 说明如何为连接字符串执行此操作。选择最适合你的。
答案 2 :(得分:0)
我建议将ConnectionString存储在App.config文件中。如下所示:
<connectionStrings>
<clear />
<add name="DefaultUniqueName" connectionString="Connection String"
providerName="Provider Name" />
<add name="UserDefine" connectionString="Connection String"
providerName="Provider Name" />
</connectionStrings>
在启动项目中,要求用户选择连接字符串选项:1)默认或2)选择新文件
如果用户选择新文件,则更新UserDefine connectionstring。
更新连接字符串(VB.NET)
Dim oAppConfig = Configuration.ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetExecutingAssembly().Location)
oAppConfig.ConnectionStrings.ConnectionStrings("UserDefine").ConnectionString = "FilePath"
oAppConfig.Save(Configuration.ConfigurationSaveMode.Minimal)
<强> C#强>
System.Configuration.Configuration oAppConfig = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetExecutingAssembly().Location);
oAppConfig.ConnectionStrings.ConnectionStrings("UserDefine").ConnectionString = "FilePath";
oAppConfig.Save(System.Configuration.ConfigurationSaveMode.Minimal);