如何使C#项目的设置成为用户特定的?

5 c# visual-studio-2013

我正在使用Visual Studio 2013编写Web API 2.0项目和测试项目.

在测试项目中,我在Settings.settings文件中保存了一些信息(在Solution Explorer中的TestProject-> Properties下).保存在其中的一件事是连接字符串到本地存储的数据库.

不幸的是,当他们下载回购时,每个人的计算机上的连接字符串会略有不同.当人们将他们的代码推送到主仓库时,它会覆盖连接字符串,影响其他人.

为每个用户配置这样的最佳方法是什么,这样每个人都可以拥有自己的数据库路径,但推送到主仓库不会影响任何人?

编辑

我不认为这与其他问题完全相同.虽然,是的,我的配置设置存储在app.config中(因为它们恰好是应用程序设置而不是用户设置),在其他答案中的解决方案将导致我出现同样的问题.app.config将包含configSource ="otherconfig.config",当人们将该文件推送到主仓库时,它仍然会破坏其他人的值.我需要一些允许自定义配置来源控制的东西,而不会影响项目的其他用户.

Ale*_*uiu 4

Visual Studio通过Web.config转换自动处理WEB项目

您需要安装一个单独的插件,以便与App.config和非Web项目一起使用.http://visualstudiogallery.msdn.microsoft.com/579d3a78-3bdd-497c-bc21-aa6e6abbc859 该插件基本上为app.config文件添加了相同的功能,并在转换文件中使用相同的语法.

您最好的方法是使用构建配置文件.拥有特定于开发人员的Web.developer.config,然后让每个用户在Configuration Manager中选择他们的名字.然后只需创建新的配置,从技术上讲,这是一个XSLT,可以为每个团队成员进行所需的更改.

可以将其视为Debug vs Release配置,除非在您的情况下您将拥有许多Debug(每个用户一个).您设置的Build配置文件不会被检入TFS,所以你很好.

这是子配置的样子:

  <?xml version="1.0" encoding="utf-8"?>

  <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <!--
      In the example below, the "SetAttributes" transform will change the value of 
      "connectionString" to use "ReleaseSQLServer" only when the "Match" locator 
      finds an attribute "name" that has a value of "MyDB".
     --> 
      <connectionStrings>
        <add name="RavenDB" connectionString="Url=http://xxx/databases/xxx" xdt:Transform="Replace" xdt:Locator="Match(name)"/>
      </connectionStrings>

    <appSettings>    
      <add key="BaseUrl" value="http://xxx" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
    </appSettings>
    <system.net>
      <defaultProxy enabled="true" />
        <mailSettings>
          <smtp xdt:TrandeliveryMethod="Network" transform="Replace">
            <network xdt:Transform="Replace" host="xxx" defaultCredentials="true" />
          </smtp>
        </mailSettings>
    </system.net>
  </configuration>
Run Code Online (Sandbox Code Playgroud)

有关web.config的更多信息转换 http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx