安装后,如何为要在我的c#.net项目中使用的源文件设置路径?

安装后,如何为要在我的c#.net项目中使用的源文件设置路径?,c#,.net,deployment,C#,.net,Deployment,在应用程序安装完成后,是否有方法提示用户设置路径 我在C#.NET中完成了一个项目,其中我的数据库文件是Excel工作簿。因为连接字符串的位置在我的编码中是硬编码的,所以在我的系统中安装它没有问题。当涉及到其他系统时,位置将不匹配。。是否有一种方法可以在安装后初始设置路径,以便所有用户都可以在不同的系统中使用它?只需在程序开始时询问即可。如果用户提供值或选择“默认值”,则设置注册表或临时文件或其他标志,并在下次需要时提醒自己 using System.IO; . . ... Main... i

在应用程序安装完成后,是否有方法提示用户设置路径


我在C#.NET中完成了一个项目,其中我的数据库文件是Excel工作簿。因为连接字符串的位置在我的编码中是硬编码的,所以在我的系统中安装它没有问题。当涉及到其他系统时,位置将不匹配。。是否有一种方法可以在安装后初始设置路径,以便所有用户都可以在不同的系统中使用它?

只需在程序开始时询问即可。如果用户提供值或选择“默认值”,则设置注册表或临时文件或其他标志,并在下次需要时提醒自己

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
.

您需要将配置文件添加到项目中

解释如何做

  • 添加app.config文件
  • 向该文件添加一些设置,例如:

    <appSettings>
        <add key="OperatorName" value="Rita"></add>
        <add key="LoggerLevel" value="5"></add>
    </appSettings>
    

    -说明如何对连接字符串执行此操作。选择最适合您的连接字符串。

    我建议将您的连接字符串存储在App.config文件中。如下所示:

    <connectionStrings>
        <clear />
         <add name="DefaultUniqueName" connectionString="Connection String"
          providerName="Provider Name" />
         <add name="UserDefine" connectionString="Connection String"
          providerName="Provider Name" />
      </connectionStrings>
    
    C#


    非常感谢您的快速回复。。。你能简要介绍一下吗,因为我对这个有点陌生。你是否将你的连接字符串存储在App.config中?显示它,如果数据库位于根目录中,则可以使用相对连接字符串否我不将其存储在App.config中。。我将其存储为:con1.ConnectionString=“Provider=Microsoft.ACE.OLEDB.12.0;数据源=//01hw149391/Mold Automation/EIS_Marketing.xls;扩展属性=Excel 12.0;”;con1.Open();
    Dim oAppConfig = Configuration.ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetExecutingAssembly().Location)
    
    oAppConfig.ConnectionStrings.ConnectionStrings("UserDefine").ConnectionString = "FilePath"
     oAppConfig.Save(Configuration.ConfigurationSaveMode.Minimal)
    
    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);