开发者

Adding custom information to CSPROJ files

开发者 https://www.devze.com 2023-02-10 04:11 出处:网络
As part of our development life cycle we have a number of process that we run against the C# source in our projects.

As part of our development life cycle we have a number of process that we run against the C# source in our projects.

The processes are driven off a GUI that currently reads the *.csproj file to find the source files used within the project. This works fine.

We now have a new requirement to provide some validation processes that require a call out to a web-service. The web-service needs to be provided with some credentials that are project specific. Ideally we could enter and store t开发者_如何学Chese credentials within the *.csproj file but I don't see a means of extending it - is there?

We don't really want to introduce a new config. file just for these settings if we can help it. Is it possible to store information like this is the *.csproj file, if not is there any other place to put it.

thanks


The .csproj file is basically an MSBuild file, as such you can extend it with custom values. If you right-click on a project in Visual Studio and choose "Unload Project", the project will "grey out" and you can then right-click again and choose Edit [ProjectFileName].csproj. You can then add something similar to the following:

<PropertyGroup Label="Custom">
  <Badger>1</Badger>
</PropertyGroup>

This should be persisted when the project is modified (i.e. files added/removed) and you can retrieve the values from the file, using the method of your choice.


VS projects support "project extensions". These are custom data stored directly in csproj/vbproj files. You can very easily read and write them even from VS. For example, the following VS macro writes such custom setting:

Dim proj As Project = DirectCast(DTE.ActiveSolutionProjects(0), Project)
proj.Globals.VariableValue("MySettingName1") = "My value1"
proj.Globals.VariablePersists("MySettingName1") = True

The following reads it back:

proj.Globals.VariableValue("MySettingName1").ToString

And the code in csproj file looks like:

  <ProjectExtensions>
    <VisualStudio>
      <UserProperties MySettingName1="My value1" />
    </VisualStudio>
  </ProjectExtensions>

Of course, this is persisted and will not be overwritten by VS.


I know you dismiss it but the most obvious, and probably recommended, place is in the config file. Albeit encrypted.

One config file per project does for most cases and is not a large overhead imho.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号