9

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 these 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

Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
BENBUN Coder
  • 4,801
  • 7
  • 52
  • 89
  • please don't use the .csproj files for specific settings such as credentials - for credentials use the web.config (or app.config), as credentials might be changed over time. – Dennis G Feb 14 '11 at 13:25
  • 1
    @moontear, unless as the OP intimated, the credentials aren't for the code, but rather for a process that "groks" through the projects. In that case, the credentials are an aspect of the project, rather than the *execution* of the code in the project! =) – Rob Feb 14 '11 at 13:28
  • Rob - you're correct the process is part of the development process and as such the credentials are part of the "source", they are nothing to do with the application when it is executing. – BENBUN Coder Feb 14 '11 at 14:23

3 Answers3

14

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.

Rob
  • 45,296
  • 24
  • 122
  • 150
  • 1
    @BENBUN - You might want to have a look at MSBuild in more detail, it's quite flexible so there's a fair chance you could define a custom "target" and have MSBuild take care of invoking the web service, etc, for you. Automation FTW ;=) – Rob Feb 14 '11 at 13:29
  • Very nearly, the "Label" attribute did not eeem to be allowed, so I ended up with : projprojproj keykeykey – BENBUN Coder Feb 14 '11 at 15:03
  • @BENBUN, "Label" as an attribute may be a VS2010/MSBuild4.0 thing (if you're using an earlier version?) as it showed in intellisense for me. Either way, hopefully it'll do what you want it to do! =) – Rob Feb 14 '11 at 15:10
4

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.

Peter Macej
  • 4,831
  • 22
  • 49
0

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.

dove
  • 20,469
  • 14
  • 82
  • 108
  • Sorry, I'm a bit of a newbie in this area. When you say "config" file do you mean I create my own or is there a project wide one available to use? – BENBUN Coder Feb 14 '11 at 13:14
  • @BENBUN: Believe he means the web.config – Theun Arbeider Feb 14 '11 at 13:20
  • Hmm - not sure if/how I use this. To be clear the web-services are not going to be used within Visual Studio, but another completely stand alone application afterwards. How would you create a web.confog file? – BENBUN Coder Feb 14 '11 at 13:32