.Net constants vs Settings vs Resources

本文探讨了在软件开发过程中如何妥善管理不常变化的字符串资源。针对不同使用场景,介绍了字符串应放置的位置,包括类内部、单独类中、Settings及Resources等,并分析了各位置的适用条件与优缺点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 参考:《When to use constants, settings.settings and Resources.resx》
  • 问题:声明一个不常改变近似“常量”的字符串时,这个字符串应该声明在何处?
  • 一般做法:
    • 字符串永不改变,只在一个类内部使用声明在这个类内部,和同一个类中的其它常量放一起
      字符串永不改变,会被多个类使用将这类字符串声明在另外一个单独的类中
      允许字符串被最终用户在配置的时候更改声明在 Settings 中

       允许字符串根据运行时的环境(如:语言)做相应的更改

      包含在一组不同的配置项中以方便配置

      (这条情况未完全遇到过)

      声明在 Resources 中
  • 其它:
    •         Resources 中的值只能在编译前修改,最终用户无法更改;Settings 中的值会集中在程序相应的配置文件中,可供最终用户修改。
    •         Settings 不能检测到运行时的本地信息(如:语言),所以“本地化”应由Resources 实现。
<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build"> <PropertyGroup> <ProjectType>local</ProjectType> <ProjectVersion>7.10.377</ProjectVersion> <SchemaVersion>2.0</SchemaVersion> <ProjectGuid>{ADC5A455-0A27-4553-B7AA-BF869BCD8EB1}</ProjectGuid> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <AssemblyName>LinuxCmd</AssemblyName> <OutputType>WinExe</OutputType> <TargetFrameworkVersion>v2.0</TargetFrameworkVersion> <IsWebBootstrapper>false</IsWebBootstrapper> <PublishUrl>publish\</PublishUrl> <Install>true</Install> <InstallFrom>Disk</InstallFrom> <UpdateEnabled>false</UpdateEnabled> <UpdateMode>Foreground</UpdateMode> <UpdateInterval>7</UpdateInterval> <UpdateIntervalUnits>Days</UpdateIntervalUnits> <UpdatePeriodically>false</UpdatePeriodically> <UpdateRequired>false</UpdateRequired> <MapFileExtensions>true</MapFileExtensions> <ApplicationRevision>0</ApplicationRevision> <ApplicationVersion>1.0.0.%2a</ApplicationVersion> <UseApplicationTrust>false</UseApplicationTrust> <BootstrapperEnabled>true</BootstrapperEnabled> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' "> <PlatformTarget>x86</PlatformTarget> <DebugSymbols>true</DebugSymbols> <DebugType>full</DebugType> <Optimize>false</Optimize> <OutputPath>bin\Debug\</OutputPath> <DefineConstants>DEBUG;TRACE</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' "> <PlatformTarget>x86</PlatformTarget> <DebugType>pdbonly</DebugType> <Optimize>true</Optimize> <OutputPath>bin\Release\</OutputPath> <DefineConstants>TRACE</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> </PropertyGroup> <PropertyGroup> <StartupObject>LinuxCmd.Program</StartupObject> </PropertyGroup> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <ItemGroup> <Compile Include="Properties\Resources.Designer.cs"> <AutoGen>True</AutoGen> <DesignTime>True</DesignTime> <DependentUpon>Resources.resx</DependentUpon> </Compile> <EmbeddedResource Include="LinuxCmd\frMain.resx"> <DependentUpon>frMain.cs</DependentUpon> </EmbeddedResource> <EmbeddedResource Include="Properties\Resources.resx"> <Generator>ResXFileCodeGenerator</Generator> <LastGenOutput>Resources.Designer.cs</LastGenOutput> </EmbeddedResource> <Reference Include="NAudio, Version=1.7.2.19, Culture=neutral, processorArchitecture=MSIL"> <SpecificVersion>False</SpecificVersion> <HintPath>.\NAudio.dll</HintPath> </Reference> <Reference Include="SDK, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL"> <SpecificVersion>False</SpecificVersion> <HintPath>.\SDK.dll</HintPath> </Reference> <Reference Include="System.Windows.Forms" /> <Reference Include="System" /> <Reference Include="System.Drawing" /> <Compile Include="AssemblyInfo.cs" /> <Compile Include="Global.cs" /> <Compile Include="LinuxCmd\LinuxEvent.cs" /> <Compile Include="LinuxCmd\BluetoothScanEvent.cs" /> <Compile Include="LinuxCmd\frMain.cs"> <SubType>Form</SubType> </Compile> <Compile Include="LinuxCmd\LinuxUdp.cs" /> <Compile Include="LinuxCmd\PlayController.cs" /> <Compile Include="LinuxCmd\Properties\Resources.cs" /> <Compile Include="LinuxCmd\DevItem.cs" /> <Compile Include="LinuxCmd\AudioEvent.cs" /> <Compile Include="LinuxCmd\RTPManager.cs" /> <Compile Include="LinuxCmd\WaveRecordDataHandler.cs" /> <Compile Include="LinuxCmd\RecordController.cs" /> <Compile Include="LinuxCmd\WaveRecorder.cs" /> <Compile Include="LinuxCmd\WaveErrorEventHandler.cs" /> <Compile Include="LinuxCmd\ATClient.cs" /> <Compile Include="LinuxCmd\LinuxSendSocket.cs" /> <Compile Include="LinuxCmd\Program.cs" /> <Compile Include="LinuxCmd\AudioManager.cs" /> <Compile Include="LinuxCmd\UdpBrocast.cs" /> <Compile Include="LinuxCmd\Properties\Settings.cs" /> <EmbeddedResource Include="LinuxCmd.Properties.Resources.resources" /> <EmbeddedResource Include="LinuxCmd.frMain.resources" /> </ItemGroup> <ItemGroup> <Content Include="LinuxCmd.ini"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> </ItemGroup> <ItemGroup> <BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1"> <Visible>False</Visible> <ProductName>.NET Framework 3.5 SP1</ProductName> <Install>true</Install> </BootstrapperPackage> </ItemGroup> </Project>
07-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值