c# - 在 Visual Studio 中使用自定义构建配置

标签 c# visual-studio buildconfiguration

在我的客户端应用程序中,我有时会连接到 localhost:1242\SomeService.asmx,有时它会连接到 someDomain:1242\SomeService.asmx。换句话说,有时我想在本地进行测试,有时又想远程进行测试。

VS 为您提供的默认选项是调试和发布。我想创建自定义的,事实上我刚刚创建了一个新的构建配置: enter image description here

无论如何,如果我正在使用该配置,我如何在代码中知道?

我想做这样的事情:

if(Configuration.Type == ConfigTypes.Local)
    ConectionString = "localhost:1242:\SomeService.asmx";
else if (Configuration.Type == ConfigTypes.Remote1)
    ConectionString = "SomeDomain1:1242:\SomeService.asmx";
else if (Configuration.Type == ConfigTypes.Remote2)
    ConectionString = "SomeDifDomain:1242:\SomeService.asmx";

Release模式也往往更高效?我将如何指定这些设置?

最佳答案

您可以定义条件编译符号。
项目属性 → 构建选项卡 → 条件编译符号
为不同的配置定义不同的符号,例如:

  • SRV_LOCAL 在“本地”配置中;
  • SRV_REMOTE1 在“Remote1”配置中;
  • SRV_REMOTE2 在“Remote2”配置中。

然后在代码中:

#if SRV_LOCAL
    private const string SERVER = "localhost";
#elseif SRV_REMOTE1
    private const string SERVER = "SomeDomain1";
#elseif SRV_REMOTE2
    private const string SERVER = "SomeDifDomain";
#endif

关于c# - 在 Visual Studio 中使用自定义构建配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32769033/

相关文章:

c# - 在csv文件的下一列中写入c#

c++ - MinGW vs Visual Studio 2008 输出代码质量

visual-studio-2010 - 使用特定的 Visual Studio 项目构建配置来运行单元测试

c# - 查询语法有效,但 lambda 语法不适用于 linq select 语句

c# - 如何让 IntPtr 访问 MemoryMappedFile 的 View ?

.net 3.5 中的 c# MemoryMappedFile

visual-studio - Nuget 包 - feed (VSTS) :Exception 'System.AggregateException' thrown when trying to add source

asp.net-mvc - Visual Studio ASP.NET MVC 项目 : setting the start URL

c# - 如何为不同的构建配置使用不同的应用程序设置?

.net - 我可以从构建配置中排除命名空间(即 Name1.Name2)吗