Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6876431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:28:35+00:00 2026-05-27T04:28:35+00:00

I would like my Windows Phone application to access an environment variable of the

  • 0

I would like my Windows Phone application to access an environment variable of the build machine.

In my scenario I specifically want to pass the USERNAME variable, so when I am looking at phone application’s analytics reports in case of debug builds – I can see which developer was running the application and see whether it was me or someone else. I could think of other similar scenarios where it would be useful to have a build-time value accessed at runtime, so I would like to find a generic solution to the problem.

I can think of one solution – use a pre-build event definition to create or update a .cs file with a constant set to the build-time environment variable value, but it seems a bit ugly and I am expecting to see problems with version control systems. I was hoping maybe there was some trick to use DefineConstants or otherwise set it using some simple msbuild project customization?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:28:36+00:00Added an answer on May 27, 2026 at 4:28 am

    You could do as you suggested: creating a .cs file with a constant during build time. To circumvent potential issues with version control systems, don’t add this file to version control and create it in a location ignored by version control (like obj\Debug or obj\Release folders).

    In MSBuild it could look like the following:

    Add the following after the final “Import”-Element in your CSPROJ file.

    <PropertyGroup>
        <!-- Make sure our file is build on every compile -->
        <CompileDependsOn>
          _CreateMyFile;
          $(CompileDependsOn)
        </CompileDependsOn>
    </PropertyGroup>
    
    <Target Name="_CreateMyFile">
    
       <!-- Add code to create the file on demand. 
       You could write a custom task, or execute some external tool.
    
       As an example assume it is created as "$(IntermediateOutputPath)\EnvVars.cs".
       Where IntermediateOutputPath expands to "obj\Debug" or "obj\Release", respectively.
       Either typically beign ignored by version control systems.
       -->
    
       <!-- Add file to files to be compiled. -->
       <ItemGroup>
           <Compile Include="$(IntermediateOutputPath)\EnvVars.cs"/>
       </ItemGroup>
    </Target>
    

    You might consider putting the above in a custom .proj file, which you import into your CSPROJ files.

    Oh, and I don’t find that ugly at all. Personally, I use it for all kinds of such tasks, like assembly version attributes, etc.

    Update:
    To prevent the target from being called on every build (or F5, etc.) you can use the Inputs and Outputs attributes of the Target element. The MSDN page is not too helpfull, but basically it works as follows: MSBuild will check if all files specified in the Output attribute are up-to-date with respect to the files specified in the Inputs attribute.

    You could thus change the target definition like so (PropertyGroup element remains the same and only added for completeness):

    <PropertyGroup>
        <!-- Make sure our file is build on every compile -->
        <CompileDependsOn>
          _CreateMyFile;
          $(CompileDependsOn)
        </CompileDependsOn>
    </PropertyGroup>
    
    <Target Name="_CreateMyFile"
       Inputs="$(IntermediateOutputPath)\EnvVars.cs"
       Outputs="$(IntermediateOutputPath)\EnvVars.cs">
    
       <!-- Create EnvVars.cs file -->
       <WriteLinesToFile
            File="$(IntermediateOutputPath)\EnvVars.cs"
            Lines="namespace Foo {}"
            Overwrite="true"
            Encoding="Unicode"/>
    
       <!-- Add file to files to be compiled. -->
       <ItemGroup>
          <Compile Include="$(IntermediateOutputPath)\EnvVars.cs"/>
       </ItemGroup>
    
       <!-- Add file to files to be removed on target "clean". -->
       <ItemGroup>
          <FileWrites Include="$(IntermediateOutputPath)\EnvVars.cs"/>
       </ItemGroup>
    </Target>
    

    The trick here is the following: if EnvVars.cs does not exist it will be created. If it does exist (then) it will no longer be created, thus not forcing a build on F5. However, we have to provide some way of getting rid of the file. Hence adding it to the FileWrites ItemGroup, which contains the files to be removed when the “clean” or “rebuild” targets (or the respective menu commands in Visual Studio) will be called. So if you want a new EnvVars.cs file, just do a rebuild or clean.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to define a theme for my Windows Phone 7 application, to
I would like to create application for Windows Phone 7, which will be communicating
In a Windows Phone 7 application, I would like to query a big XML
I would like to update a Windows Forms application to provide the following features:
I would like to create a Delphi application for Windows XP which allows dropping
I'm writing an application for a Windows Mobile phone in VB.Net, and I would
I would like to develop for Windows Phone 7 but the lack of database
I have developed two Windows Phone applications and would like to deploy them on
I would like to declare some data in my Silverlight for Windows Phone 7
I am having problems debugging a Windows Phone XNA game application. I can build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.