How to run command line code from within C# Windows Form? - c#

I am fairly new to coding, but have built a few small things. One thing I figured out on my last project was how to run 2 simple commands normally run from a console, but from within a form application instead. Simply, the form had 2 buttons and clicking one caused ipconfig to run and the other ipconfig /all. It then posted the ip information coming from the command into another form I created as a message box. That is important because I am trying to do something similar and nothing is working now.
I have a form that has a spot for user name and a spot for password. On submit, I want it to essentially run the following:
NET USE F: \\ALPHA\CLIENTAPPS /user:domain\%username% %password% /persistent:no
NET USE O: \\ALPHA\USERS /user:domain\%username% %password% /persistent:no
NET USE S: \\ALPHA\COMPANY /user:domain\%username% %password% /persistent:no
Where %username% and %password% are captured from the form and domain will be our actual domain.
Using similar methods to the aforementioned ipconfig program that is working, this is what I came up with. However, when I click the Submit button, nothing happens, no errors, nor does it actually create the network share:
private void btnSubmit_Click(object sender, EventArgs e)
{
string un = txtUsername.Text;
string pw = txtPassword.Text;
System.Diagnostics.ProcessStartInfo PR = new System.Diagnostics.ProcessStartInfo("cmd", #" /c net use W: \\\\ALPHA\\CLIENTAPPS /user:acsdish\\" + un + " " + pw + "/persistent:no");
PR.RedirectStandardOutput = true;
PR.UseShellExecute = false;
PR.CreateNoWindow = true;
System.Diagnostics.Process StartPR = new System.Diagnostics.Process();
StartPR.StartInfo = PR;
StartPR.Start();
}
What am I missing here, or is there a better way? Thanks.
Mike

System.Diagnostics.ProcessStartInfo PR = new System.Diagnostics.ProcessStartInfo("cmd", #" /c net use W: \\\\ALPHA\\CLIENTAPPS /user:acsdish\\" + un + " " + pw + "/persistent:no");
Try to remove "#" or remove escaping of "\" char
Info here (Verbatim string literals)

nothing happens, no errors, nor does it actually create the network share
You've done a lot to ensure that. "No errors" is easy to explain, you don't check for errors nor do you give a way for the user to see them because you made sure that the console window isn't visible. If the command failed that it won't be visible. Checking Process.ExitCode is a minimal requirement.
Next flaw is that you create the mapping to the share for a particular user. Which is fine, drive mappings are a per-user setting. But you are not actually logged-in as that user so you can't see those mappings. You'll have to hit Ctrl+Alt+Del and switch the user account. But that's a lost cause because you passed /persistent:no. That means "persistent while the user is logged in".
Ultimate flaw is that you leave it up to an another process to take care of it. That always loses critical information, especially errors. You should pinvoke the Windows api function that does this so you know when it doesn't work and don't burn a gazillion cycles to run another process. Pinvoke WNetAddConnection2().

Related

Having issues getting entire output from StandardOutput.ReadToEnd

At work we have a large number of on-site WES09 machines that are doing controls data gathering. Because we have thousands of these, upgrades are a pain. I have been slowly evolving a better process, and am currently working on a C# application to better manage these.
The machines are running File Based Write Filter, which has the effect that if PSEXEC had not been run on the machine before, it won't let it start. So I use WMIC to disable FBWF.
I am trying to add a status feature to see if FBWF is enabled, as I haven't always had 100% success with WMIC. Anyway, FBWF gives status output in the command window, but it's multiple lines. It seems when I use StandardOutput.ReadToEnd, it only gets the first line.
So here is what it looks like when you check FBWF status from PSEXEC:
FBWFOutput
When I run the C# Application I wrote, the output only gets this:
"\r\nPsExec v2.2 - Execute processes remotely\r\nCopyright (C)
2001-2016 Mark Russinovich\r\nSysinternals -
www.sysinternals.com\r\n\r\nFile-based write filter configuration for
the current session:\r\n"
I don't know why it stops there, I can't get the rest of it that actually says if it is enabled or not (which is the next line, of course).
Here is the relevant code piece, leaving out IP/User/Pass:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.FileName = #"C:\temp\PSTools\PsExec.exe";
p.StartInfo.Arguments = #"\\" + RemotePCName + " -u " + Username + " -p " + Password + " fbwfmgr";
p.Start();
string output = p.StandardOutput.ReadToEnd();
string errormessage = p.StandardError.ReadToEnd();
I thought my issue might be the line break, but there looks to be two between the PsExec portion and the FBWF output, but otherwise am not sure what else to try. I spent a couple hours looking through other comments on issues with getting the output, but almost all were that the application would hang which is not an issue I am having.
Thank you in advance for the assistance, I am slowly learning C# and programming in general and this community has been a huge help!

Elevated executable can't find OS Tool

I'm trying to create a shortcut in my C# WinForm application for users to launch Microsoft's "Digitizer to Monitor Mapping Tool" (MultiDigiMon.exe). However it seems that even if ran as an administrator my applications or cmd processes launched from it can not find the executable in question and I have no idea why.
My Short cut consists of a simple button with the click event hooked to the fallowing function.
private void TouchButton_Click(object sender, EventArgs e)
{
ProcessStartInfo start = new ProcessStartInfo("C:\\Windows\\System32\\cmd.exe");
start.WorkingDirectory = "C:\\Windows\\System32";
start.CreateNoWindow = false;
start.UseShellExecute = false;
//start.Arguments = "/K \"cd C:\\Windows\\System32 && MultiDigiMon.exe -touch\"";
Process.Start(start);
}
In my experience on Win10 systems, MultiDigiMon.exe can be always be found in C:\Windows\System32 (assuming the C drive is your root). Additionally the arguments line above was commented out to make the following screen shots as identical as possible.
On the left is the command prompt launched using the above code and on the right is a command prompt launched by typing cmd in to the OS's taskbar and and running the first result as administrator. As you can see both promps are marked as administrator but the left fails to launch MultiDigiMon.exe and won't even list the file exist using the dir command. the right seems to have no issue with either command. (MultiDigiMon.exe dose not seem to do anything at all if multiple touch screen devices are not connected and I do not believe that it give any text output especially if successful).
Finally I have also checked the permission on MultiDigiMon.exe and (with the exception of TrustedInstaller who has full control) everybody has read and execute right. Could someone explain why I can't launch MultiDigimon.exe form the left prompt or directly from my application?
As commented on by RbMm the issue was that since my application was being compiled to run on any cpu it was treated as a 32 bit application by the file system and redirected all access to the System32 directory to SysWOW64.
To get around this you can substitute "System32" width "Sysnative" as discussed here. In my case my final code ended up looking like the fallowing.
private void TouchButton_Click(object sender, EventArgs e)
{
ProcessStartInfo start = new ProcessStartInfo("C:\\Windows\\Sysnative\\MultiDigiMon.exe");
start.WorkingDirectory = "C:\\Windows\\System32";
start.CreateNoWindow = false;
start.UseShellExecute = false;
start.Arguments = "-touch";
Process.Start(start);
}

UWF_Volumes has no entries with CurrentSession=False

since some time now I try to figure out how to correctly setup this new UWF (Unified Write Filter). Unfortunately it seems there is only documentation for Win 8.1 industry (here), not for Win 10. I hope there were no relevant changes since.
I also asked this on the WindowsDevCenter but got no response so far.
Here is my problem:
With the WMI providers I got UWF enabled by now (UWF_Filter.Enable()), but I cannot protect any volume.
Also the volume list looks very strange: There are 4 entrys, everyone is with CurrentSession=True.
The first is for an volume with no drive letter, only a volume id.
The second is for C:
and then there are 2 identical for D: .
Should'nt there normally be 2 entrys per volume, one where CurrentSession is true and one where its false, meaning its the setting applied after reboot?
If I try to execute Protect on the ManagementObject with DriveLetter=C: I get an Access denied exception, I assume because its the object for the current session.
Also if I try uwfmgr.exe Volume Protect C: on the console it simply hangs: no reaction, no error, only a forever blinking cursor. EDIT: it turned out this was a problem caused by another installed software. See also below.
Do I have to enable or disable or do anything else before I can protect volumes?
Thanks in advance,
Sebastian
My system:
Windows 10 IOT Enterprise 2016 LTSB x64
1 SSD 250GB with Boot, C: and D:
Edit:
Here I asked a follow up question with some other details and a workaround. If I use uwfmgr.exe volume protect c: for example, it works and UWF_Volume now suddenly has (the correct) 2 entries for C:, one for the current and one for the next session.
However I want to avoid this, because IMHO it should be solveable by WMI only.
Edit 2: #sommmen
The partition layout is as following: One disk with 4 partitions.
Boot, 500MB
C:/ , 45GB
unknown, 500MB (Boot-Backup I think)
D:/ , ~200GB
PS:
Please could anyone create the tags uwf and uwfmgr? Would be nice :-)
Missing UWF_Volume instances often appeared after reboot in my tests. But if not, you can create them directly using ManagementClass.CreateInstance().
The problem here is that the official docs are not exactly correct. The description of the UWF_Volume.VolumeName property is:
The unique identifier of the volume on the current system. The
VolumeName is the same as the DeviceID property of the Win32_Volume
class for the volume.
from: https://learn.microsoft.com/en-us/windows-hardware/customize/enterprise/uwf-volume#properties
In fact, the DeviceID needs a slight modification, before using it as value for UWF_Volume.VolumeName:
DeviceID.Substring(4).TrimEnd('\\')
So, after removing prefix \\?\ and removing any trailing slashes you can create instances with CurrentSession=false for the specified device.
This also works in Windows 10 Pro without any uwfmgr.exe. Though, officially not recommended/supported.
Also, I was not able to delete instances, yet. So be sure to add only correct values.
Full Example:
// example value
var DeviceId_From_Win32_Volume = #"\\?\Volume{c2eac053-27e3-4f94-b28c-c2c53d5f4fe1}\";
// example value
var myDriveLetter = "C:";
var myDeviceId = DeviceId_From_Win32_Volume.Substring(4).TrimEnd('\\');
var wmiNamespace = "root\\standardcimv2\\embedded";
var className = "UWF_Volume";
var mgmtScope = new ManagementScope {Path = {NamespacePath = wmiNamespace}};
var mgmtPath = new ManagementPath(className);
var mgmtClass = new ManagementClass(mgmtScope, mgmtPath, null);
// prepare the new object
var newObj = mgmtClass.CreateInstance();
newObj.SetPropertyValue("DriveLetter", myDriveLetter);
newObj.SetPropertyValue("VolumeName", myDeviceId);
newObj.SetPropertyValue("CurrentSession", false);
newObj.SetPropertyValue("CommitPending", false);
newObj.SetPropertyValue("BindByDriveLetter", false);
// create the WMI instance
newObj.Put(new PutOptions {Type = PutType.CreateOnly});
I experience the similar issue in that I could not query the UWF_Volume with CurrentSession=False. However, there's one thing I did that seems to "generate" the UWF_Volume management object with CurrentSession=False. I ran "uwfmgr volume protect c:". Unfortunately, in your case running this causes it to hang.
Could you try running uwfmgr in cmd in admin? Also, if you run "uwfmgr get-config", would you be able to get the current setting of the write filter?
Another thing from your description: you said there are two identical volumes for D:, but if you looks closely at the properties, one would be CurrentSession=True, and the other one is CurrentSession=False. According to the documentation, if you want to make change, you must select the management object (UWF_Volume) with CurrentSession=False.
https://learn.microsoft.com/en-us/windows-hardware/customize/enterprise/uwf-volume
(scroll down to powershell script code sample section)
First of all a volume may have several partitions. They will show up as having the same drive label.
e.g.
C:/ //?/{some guid here}
C:/ //?/{some other guid here}
Now this is common for the %systemDrive% because this has the boot partition.
You can use the commands
mountvol
and
Diskpart
List volume
To figure out the right guid for your need (or you can protect both the boot partition and the system partition). Also using wmi you can look at Win32_volume under namespace cimv2 to get some more insight.
The command line util UWFmgr seems to create an UWF_VOLUME wmi instance once you run the protect command. The docs also hint that you need to create an object yourself.
function Set-ProtectVolume($driveLetter, [bool] $enabled) {
# Each volume has two entries in UWF_Volume, one for the current session and one for the next session after a restart
# You can only change the protection status of a drive for the next session
$nextConfig = Get-WMIObject -class UWF_Volume #CommonParams |
where {
$_.DriveLetter -eq "$driveLetter" -and $_.CurrentSession -eq $false
};
# If a volume entry is found for the drive letter, enable or disable protection based on the $enabled parameter
if ($nextConfig) {
Write-Host "Setting drive protection on $driveLetter to $enabled"
if ($Enabled -eq $true) {
$nextConfig.Protect() | Out-Null;
} else {
$nextConfig.Unprotect() | Out-Null;
}
}
=======> (!) im talking about this comment
# If the drive letter does not match a volume, create a new UWF_volume instance
else {
Write-Host "Error: Could not find $driveLetter. Protection is not enabled."
}
}
The docs however do not provide a method of doing this. For now it seems we need to use the command line util till someone has an example using the WMI provider.
To answer my own question: So far I have only a workaround but no real solution.
It is to check if there is an entry with CurrentSession=False and if not invoke the command directly:
ManagementObjectSearcher ms = new ManagementObjectSearcher(_Scope, new ObjectQuery("select * from UWF_Volume where VolumeName = \"" + volId + "\" AND CurrentSession=\"False\""));
ManagementObjectCollection c = ms.Get();
UInt32 res = 1;
foreach (ManagementObject mo in c)
{
// entry found: do it with WMI
res = (UInt32)mo.InvokeMethod(newState ? "Protect" : "Unprotect", new object[] { });
}
if (c.Count == 1 && res == 0)
// message: success
if (c.Count == 0)
{
// no entry found: invoke cmd
ProcessStartInfo info = new ProcessStartInfo("uwfmgr.exe", "volume " + (newState ? "Protect" : "Unprotect") + #" \\?\" + volId);
Process process = new Process();
info.Verb = "runas"; //needs admin
process.StartInfo = info;
process.Start();
process.WaitForExit();
}
This has the side effect that for a split second a command line window will pop up, but nevertheless it works well.

Launch minecraft 1.6.4 from command line with C#

I'm currently working on a custom Minecraft 1.6.4 launcher.
I have written most of the code is C# however I'm writing the final piece of code in batch.
This seems to work to launch the minecraft 1.5.2 jar
cd "%~1"
java -Xms512m -Xmx1g -Djava.library.path=Bin\bin\native\ -cp Bin\bin\* net.minecraft.client.Minecraft %~2 %~3
pause
(C# Works out the session ID (%~3))
However in minecraft 1.6.4 I get an error:
Error: Could not find or load main class net.minecraft.client.Minecraft
I have heard about a change in the way Minecraft launches, is there anyway for me to implement the new system into my launcher?
If there is a way to do it without batch that would be awesome!
C# code to launch the batch file:
string sessionID = Session.DoLogin(tbUser.Text, tbPassword.Text).SessionId;
Process p = Process.Start("launch.bat", #"""" + filePath + #"""" + " " + tbUser.Text + " " + sessionID);
p.WaitForExit();
I was the first to make a C# launcher for minecraft and I'm happy to share some experiences. I'm sorry to reply to you so late!
Note that you are looking to start the class "net.minecraft.client.main.Main" instead of "net.minecraft.client.Minecraft". I came across this as well when I had to port my launcher to 1.6. It still works for 1.7.4 currently.
The example below is a cleaned up batch command straight from my (EDIT: OLD, but gold) open source launcher
Minecraft 1.7.4 batch launch (jvm arguments):
java
-Djava.library.path=C:\Users\<username>\AppData\Roaming\.minecraft\libraries\org\lwjgl\lwjgl\lwjgl-platform\2.9.0\
-cp
C:\Users\<username>\AppData\Roaming\.minecraft\versions\1.7.4\1.7.4.jar;
C:\Users\<username>\AppData\Roaming\.minecraft\libraries\java3d\vecmath\1.3.1\vecmath-1.3.1.jar;
C:\Users\<username>\AppData\Roaming\.minecraft\libraries\net\sf\trove4j\trove4j\3.0.3\trove4j-3.0.3.jar;
C:\Users\<username>\AppData\Roaming\.minecraft\libraries\com\ibm\icu\icu4j-core-mojang\51.2\icu4j-core-mojang-51.2.jar;
C:\Users\<username>\AppData\Roaming\.minecraft\libraries\net\sf\jopt-simple\jopt-simple\4.5\jopt-simple-4.5.jar;
C:\Users\<username>\AppData\Roaming\.minecraft\libraries\com\paulscode\codecjorbis\20101023\codecjorbis-20101023.jar;
C:\Users\<username>\AppData\Roaming\.minecraft\libraries\com\paulscode\codecwav\20101023\codecwav-20101023.jar;
C:\Users\<username>\AppData\Roaming\.minecraft\libraries\com\paulscode\libraryjavasound\20101123\libraryjavasound-20101123.jar;
C:\Users\<username>\AppData\Roaming\.minecraft\libraries\com\paulscode\librarylwjglopenal\20100824\librarylwjglopenal-20100824.jar;
C:\Users\<username>\AppData\Roaming\.minecraft\libraries\com\paulscode\soundsystem\20120107\soundsystem-20120107.jar;
C:\Users\<username>\AppData\Roaming\.minecraft\libraries\io\netty\netty-all\4.0.10.Final\netty-all-4.0.10.Final.jar;
C:\Users\<username>\AppData\Roaming\.minecraft\libraries\com\google\guava\guava\15.0\guava-15.0.jar;
C:\Users\<username>\AppData\Roaming\.minecraft\libraries\org\apache\commons\commons-lang3\3.1\commons-lang3-3.1.jar;
C:\Users\<username>\AppData\Roaming\.minecraft\libraries\commons-io\commons-io\2.4\commons-io-2.4.jar;
C:\Users\<username>\AppData\Roaming\.minecraft\libraries\net\java\jinput\jinput\2.0.5\jinput-2.0.5.jar;
C:\Users\<username>\AppData\Roaming\.minecraft\libraries\net\java\jutils\jutils\1.0.0\jutils-1.0.0.jar;
C:\Users\<username>\AppData\Roaming\.minecraft\libraries\com\google\code\gson\gson\2.2.4\gson-2.2.4.jar;
C:\Users\<username>\AppData\Roaming\.minecraft\libraries\com\mojang\authlib\1.2\authlib-1.2.jar;
C:\Users\<username>\AppData\Roaming\.minecraft\libraries\org\apache\logging\log4j\log4j-api\2.0-beta9\log4j-api-2.0-beta9.jar;
C:\Users\<username>\AppData\Roaming\.minecraft\libraries\org\apache\logging\log4j\log4j-core\2.0-beta9\log4j-core-2.0-beta9.jar;
C:\Users\<username>\AppData\Roaming\.minecraft\libraries\org\lwjgl\lwjgl\lwjgl\2.9.1-nightly-20131120\lwjgl-2.9.1-nightly-20131120.jar;
C:\Users\<username>\AppData\Roaming\.minecraft\libraries\org\lwjgl\lwjgl\lwjgl_util\2.9.1-nightly-20131120\lwjgl_util-2.9.1-nightly-20131120.jar;
C:\Users\<username>\AppData\Roaming\.minecraft\libraries\org\lwjgl\lwjgl\lwjgl-platform\2.9.1-nightly-20131120\lwjgl-platform-2.9.1-nightly-20131120-natives-windows.jar;
C:\Users\<username>\AppData\Roaming\.minecraft\libraries\net\java\jinput\jinput-platform\2.0.5\jinput-platform-2.0.5-natives-windows.jar;
C:\Users\<username>\AppData\Roaming\.minecraft\libraries\tv\twitch\twitch\5.12\twitch-5.12.jar;
C:\Users\<username>\AppData\Roaming\.minecraft\libraries\tv\twitch\twitch-platform\5.12\twitch-platform-5.12-natives-windows-64.jar;
C:\Users\<username>\AppData\Roaming\.minecraft\libraries\tv\twitch\twitch-external-platform\4.5\twitch-external-platform-4.5-natives-windows-64.jar;
net.minecraft.client.main.Main
--username=<your username>
--accessToken <your accesstoken>
--version 1.7.4
--gameDir C:\Users\<username>\AppData\Roaming\.minecraft
--assetsDir C:\Users\<username>\AppData\Roaming\.minecraft\assets
--userProperties {}
--uuid <your uuid>
You can also find out how to get the uuid, session token, and so on on in the same namespace in the link above , or HERE.
This might help:
http://s3.amazonaws.com/Minecraft.Download/versions/1.6.4/1.6.4.json
The link format works for all minecraft versions so you can compare the information and check what has changed.
Be also aware that the Minecraft authentication system has changed. (I think you have to use the new system since 1.7 but it might apply for 1.6 too)
Edit:
You also might want to decompile the official launcher (I did that for my own launcher) provides some useful information about how stuff works.

Changing environment variables of the calling process

This one seems trivial but the answer has eluded me for a few days now.
I have a Windows batch file, that calls a C# program to do an extra verification that cannot be done in a batch file. After the verification is complete I need to return a status and a string back to the calling shell.
Now the return value is trivial and my C# console app simply sets a return value (exit code if you will). And I thought the string will also be a piece of cake. I attempted to define a new shell variable using the:
Environment.SetEnvironmentVariable("ERR", "Some text");
This call should (and does) define a shell variable within the current process - that is the very C# process that created the variable. The value is lost as soon as the C# app terminates and the shell that created the C# app knows nothing about the variable. So... A call with no particular use... At all... Unless perhaps if I created a child process from the C3 app, perhaps it would inherit my variables.
The EnvironmentVariableTarget.Machine and EnvironmentVariableTarget.User targets for the SetEnvironmentVariable call don't solve the problem either, as only a newly created process will get these new values from the registry.
So the only working solution I can think of is:
write to stdout
write to a file
encode extra meaning into the return value
The first two are a bit ugly and the last one has its limitations and problems.
Any other ideas (how to set a shell variable in the parent process)? Maybe such shell variable modifications are a security concern (think PATH)...
Thank-you for your time.
I had the same problem as Ryan and the only thing that came to my mind as a work-around was to write a batch in error out to set the variable and to call it from the batch.
ConsoleApplication1.exe:
'put some sensible code here
'put result in variable myResult
Dim myResult As String = Guid.NewGuid().ToString("D").ToUpperInvariant()
Console.WriteLine("Normal output from the consonle app")
Console.Error.WriteLine("#ECHO OFF")
Console.Error.WriteLine("SET zzzResult={0}", myResult)
Test.cmd (the calling batch):
#ECHO OFF
:Jump to folder of batch file
PUSHD %~d0%~p0
:Define a temp file
SET zzzTempFile=%TEMP%\TMP%Random%.CMD
:Call .NET console app
ConsoleApplication1.exe 2>%zzzTempFile%
:Call the generated batch file
CALL %zzzTempFile%
:Clean up temp file
DEL %zzzTempFile%
:Clean up variable
SET zzzTempFile=
:Do something with the result
ECHO Yeah, we finally got it!
ECHO:
ECHO The value is "%zzzResult%".
ECHO:
:Clean up result variable
SET zzzResult=
:Go back to original folder
POPD
That should do the trick. And yes, I do know this is an old post and Ryan is solving other issues by now, but there might be still somebody else out there having the same problem...
What you are asking is to be able to arbitrarily write to the memory space of a running process. For good reason, this is not possible without SeDebugPrivilege.
Any of the three solutions you list will work. Stdout is the standard way to communicate with a batch script.
By the way, you're writing a Windows batch file. I'm pretty sure the ship has already sailed on "a bit ugly".
If you want to put a value of some output into a variable in the batch you can use the following construct:
FOR /F "usebackq tokens=4 delims=\[\] " %i IN (`ver`) DO set VERSION=%i
ECHO %VERSION%
Output on my OS:
6.1.7601
'usebackq' means we are using back quotes which gives the ability to use a fileset in the command quoted with double quotes. You may not need this. 'tokens' means the index in the resulting string array to select (it can be a range M-N). If you need to skip lines use 'skip=X'). 'delims' are the string separators to use (like string-Split() in .Net).
You will put your console app instead of 'ver' and adapt the delimiters and tokens to match your specific output. If you have more variables to fill you will need to make the if a bit more complex but that should make a good start.
My BAT is a bit rusty, but I think it's possible to retrieve the 'exit' code from processes you've run externally, perhaps via %ERRORLEVEL%. If that's the case, make sure to exit your program via
Environment.Exit(123); // where 123 = error code
You can't add any messages, so you'll have to do that in the .bat file.
If this isn't the case, stdout is probably the best way.
After stumbling on this myself as well recently, I came up with this approach. What I did is run the bat file using the Process class, i.e.
// Spawn your process as you normally would... but also have it dump the environment varaibles
Process process = new Process();
process.StartInfo.FileName = mybatfile.bat;
process.StartInfo.Arguments = #"&&set>>envirodump.txt";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = false;
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
// Read the environment variable lines into a string array
string[] envirolines = File.ReadAllLines("envirodump.txt");
File.Delete("envirodump.txt");
// Now simply set the environment variables in the parent process
foreach(string line in a)
{
string var = line.Split('=')[0];
string val = line.Split('=')[1];
Environment.SetEnvironmentVariable(var, val);
}
This seems to have worked for me. It's not the cleanest approach, but will work in a bind. :)

Categories

Resources