environmental variable in a c# - c#

I am using the following command to set a value to the environmental variable in a c# Console application.
System.Environment.SetEnvironmentVariable(envvar, result,EnvironmentVariableTarget.Process);
After running the application in the command window, when I try to echo that variable ,I cannot see the value.
I have to use this application in a batch file.
I want the functionality like SET command. Please help..
Edit:
I tried using System.Environment.SetEnvironmentVariable(envvar,result,EnvironmentVariableTarget.user) and to propagate the change I tried this Propagating Change in Env VAr. But I cant echo the variable in same command window.
Let me rephrase the question:
I want to set a value to a Env Var in c#. I must be able to use that variable in same command window (ie i should not open a new cmd window to see the change). We use SET command and we can use that variable immediately .. rt ? I want such functionality. Plzz help

When you use EnvironmentVariableTarget.Process the variable set will only visible in the current process as you can see in this sample:
System.Environment.SetEnvironmentVariable("myVar", "myValue", EnvironmentVariableTarget.Process);
string s = System.Environment.GetEnvironmentVariable("myVar",EnvironmentVariableTarget.Process);
Above myVar will show s = "myValue" but not visible in command window.
If you want to set the value visible at command windows then you need to use EnvironmentVariableTarget.User:
System.Environment.SetEnvironmentVariable("myVar", "myValue", EnvironmentVariableTarget.User);
This way the setting myVar=myValue will be stored and then you can see on command windows.
A detailed sample is located here

In order to see the env in the current batch process. You have to output it in you program as string and parse it and call set in the batch file.
Or you can try EnvironmentVariableTarget.User. The env will be visible in all new processes when setted with this option.

Related

Pass parameters with apostrophs to a batch file

How do I passt following parameters to my batch file?
custom.bat mode="test" logs="true"
I tried to double the " but nothing helped.
custom.bat "mode="test"" "logs="true""
And, in custom.bat you remove the unneeded quotes
#echo off
set "arg1=%~1"
set "arg2=%~2"
echo [%arg1%] [%arg2%]
You may use CALL command to launch a new batch-file. After executing the last line of the "called file", the control will return back to the "calling file".
You may set the parameters to the "called .bat fie" by using either a simple string or a variable.
eg.
CALL MyScript.bat "1234"
or
SET _MyVar="1234"
CALL MyScript.bat %_MyVar%
As a precaution, you may use SETLOCAL & ENDLOCAL to keep separation between variables of same-name among different files.

Pass value from command line to Custom action-Burn

I am in a need to pass the value from command line to custom action.The custom action should get the value from command line and modify one file(app.config) during installation of EXE.I have the below code in custom action
if (Context.Parameters["ENV"] == "test") //Test environment
{
authEndPoint = "http://192.168.168.4/api/user_authentication/";
}
else if (Context.Parameters["ENV"] == " ") //Production environment
{
authEndPoint = "https://livesiteurl/api/user_authentication/";
}
I want to install the exe file like below
myapplication.exe env=test
I seen lot of sample to pass the value from command line to msi. How to pass the value from command line to CA and modify the app.config file.
There are better ways to do what you're trying to do here than use a custom action. Take a look at this for how use the WiXUtilExtension to modify the file, then create a property and reference it from the command line. If you still need/want to use a bootstrapper you can set that property you created with MsiProperty inside MsiPackage in the bundle.

Viewing Properties.Settings.Default variables

I am trying to use Settings.settings to define/persist some vars. For brevity, I've set up a test file to demonstrate the behavior I'm seeing:
First, I define a setting in Settings.settings:
I then have the following code to test changing variableName:
public Form1()
{
InitializeComponent();
string newString = Properties.Settings.Default.variableName;
Properties.Settings.Default.variableName = "This is a new string";
Properties.Settings.Default.Save();
}
Running the above in the debugger for the first time, I grab the current value (the value I set in the Settings.settings window initially) of variableName from Properties.Settings. As expected, newString is set to "This is a string". Good.....
After executing the next two lines, the debugger shows variableName changed to "This is a new string". Good....
I then run the app through the debugger again. I hit the string newString line and, prior to execution, newString is undefined (of course). Good....
As soon as I execute...
string newString = Properties.Settings.Default.variableName;
... and on subsequent executions of the code, the actual value of variableName is defined as "This is a new string" (Good...as expected).
I then go back to the Settings.settings window. variableName has not changed - it's still "This is a string". I've even closed VSE 2012 and re-opened the project. Settings.settings never changes.
Where is the new value being stored? I've checked all of the .config files ([appname].exe.config, [appname].vshost.exe.config, app.config, and the Settings.settings file) and the new value, "This is a new string" isn't anywhere to be found.
In summary, I'm getting the result I desire from the code, but I can't seem to view the result at design time other than to check the value of the var in the debugger. This seems not only peculiar to me, but impossible.
What am I missing/where am I not looking? I would fully expect the value of variableName to change in the Settings.settings window, but it never does. I've looked everywhere on StackOverflow/Google and can't seem to find the answer.
Thanks in advance....
The original value that you configured via Settings.settings is stored in a .config file alongside your executable's assembly. This will never change unless you modify the Settings file directly via Visual Studio; it's essentially a read-only file.
The user's customized setting is stored in a separate config file within the user's profile. The location of this file depends on your assembly's metadata. For example, on Windows 7/Vista the location might look like:
C:\Users\<user name>\AppData\Local\<company name>\<assembly name>\
AssemblyName\<version>\user.config
If you haven't customized the company name in your assembly's metadata then it may default to Microsoft. Also note that AppData is a hidden folder that may not be visible in Windows Explorer depending on your view settings.
I am not sure if I understand your question. That variable content stay persistent. Thats it. Why you would set a persistent variable to change it later?

writing environment variable and reading it from the same process

I am trying to set environment variable from a console application by executing it from my windows application. I invoke console application and send the value of environment variable as parameter to it, then set the thread to wait for 10 seconds to proceed with next execution.
In the next step i try to load a new .exe which reads the value set to the environment variable.
The exe will not read the new value and continue to refer the value set earlier.
Once the solution of application is closed and open then it reads the new value i.e reload the vshost.
betting you set up the variable only for the current process. You should try this overload of the Environment.SetEnvironmentVariable method :
Environment.SetEnvironmentVariable("YourVar", "YourValue",
EnvironmentVariableTarget.User);
[Edit] Re-Reading your question, you said in the title "same process", and in the question "new exe". In term of Env varialble, spanning a new process implies a new process scope for env variables. They won't share env variables with process scope just because it's the same executable. Maybe you should explain what you are trying to do at a higher level.
[Edit2] not sure to understand why it fails... But you can specify env variable when spawning process using a ProcessStartInfo.EnvironmentVariables Property
Basically, it can be (not tested) :
var psi = new ProcessStartInfo {
FileName="yourExe"
};
psi.EnvironmentVariables.Add("YourVariable","YourValue");
var process = Process.Start(psi);

Is it possible to use global options in P4.NET?

I am using P4.NET to control perforce programmatically but there doesn't seem to be a way to specify global options like this:
http://www.perforce.com/perforce/doc.current/manuals/cmdref/o.gopts.html
Maybe someone else has an experience on how to do this?
You can set the global environment values using the P4Connection class like so
P4Connection p4 = new P4Connection();
p4.Host = "127.0.0.1";
p4.Port = 1666;
p4.User = "joan.verge";
p4.Client = "joanverge_main";
If these values are not set, it will use the environment settings set in your client configuration file or windows registry (for Linux and OSX there is a configuration file in the ~.p4 directory).
If you use Perforce via the command line I would highly recommend you set up a client configurations (Note: I only know how to do this on Windows, refer to P4 KB for other platforms).
First set your global environment settings using the command line.
p4 set P4USER=joan.verge
p4 set P4PORT=127.0.0.1:1666
To test the above have been set correctly, use p4 info.
Next, put the workspace/client specific settings into a text file named p4config (no extension) and place it in the root of your workspace/client.
P4CLIENT=joanverge_main
Set one more environment variable,
p4 set P4CONFIG=p4config
Copy the p4config file to another workspace/client, edit the P4CLIENT variable to match.
Now on the command line, navigate to both directories and compare the p4 info command.
Also, setting this up allows you to use the P4 windows explorer navigation across multiple workspaces.
Edit: you might find this StackOverflow question useful that was answered by Mike a few months ago with setting the Charset property: P4.Net and P4CHARSET.

Categories