Per user code values in Visual Studio
I'm working on a project that I will release as open source, but it will have to contain some values (BASS audio library freeware regkey, url for update server, etc.) that I only want to be compiled into the source when compiling it for a binary release for those people that just want to use the application and do not care about the source. Basically, I want to prevent people who use the source code from using my reg key (they can get their own for free) or having their version contact my update server since versions will be different.
So, what's the best way to make something that will be compiled into the code and used if the file exists, but I could then exclude from 开发者_如何学Csource control and it would just use defaults or disable compilation of certain sections if that file does not exist?
You could edit the project file to conditionally include either one file or another based on the build type. I don't have a sample to hand, but it shouldn't be too hard to do. Something like:
<ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
<Compile Include="PublicFile.cs" />
</ItemGroup>
<ItemGroup Condition=" '$(Configuration)' == 'ApplicationRelease' ">
<Compile Include="PrivateFile.cs" />
</ItemGroup>
I would try to structure the "library" part separately from the "application" part, and always make the library part take the relevant parameters in its configuration. That way anyone just wanting to use the library should be able to use a "release" DLL - it's only the application which needs the extra file before it'll work.
EDIT: It's entirely possible that the same approach will work with app.config
if you want to include the values in a config file instead.
How I would go about doing this:
- Place the variable elements into the app.config file and in your source control have default values.
- Have the application load the values from app.config
- Either include instructions on how to change the defaults, or have a small wrapper that updates the defaults the first time the app is run (for situations where potential users would not have the knowledge to edit app.config).
精彩评论