0

I have a C# program that calculates a date. I want to set an environment variable datayyyymmdd to be read outside the program to be expaned into a filename that I need to look for with the Dos code.

Can someone please help me with c# code sample or any link .

Thanks

Andriy M
  • 76,112
  • 17
  • 94
  • 154
vish1990
  • 272
  • 3
  • 16
  • 3
    Dos?! Did you manage to run .Net on Dos??! :-D – AK_ Jun 20 '14 at 13:57
  • 1
    possible duplicate of [How do I get and set Environment variables in C#?](http://stackoverflow.com/questions/185208/how-do-i-get-and-set-environment-variables-in-c) – ElGavilan Jun 20 '14 at 13:58
  • I have a C# program that calculates a date. I want to set an environment variable datayyyymmdd to be read outside the program to be expaned into a filename that I need to look for with the Dos code. To be more specific: input arguements for abc are x and y. I’d like to basically do like this… calling my program (say abc.exe") that give date from batch file and in that batch file i wish to add something like this : Set var1=[call abc.exe.exe “E:\\flags\\reportVersions” “x” “y” var1 should have the date that comes from abc.exe – vish1990 Jun 20 '14 at 14:17

2 Answers2

1

This is not a trivial task as each process has its own environment variables table, especially if the environment variable should not be set permanently for all running processes.

The solution is quite simple.

In your C# application create a batch file with the line:

set YourVar=datayyyymmdd

The C# application could create this batch file with name "SetYourVar.bat" in directory for temporary files. The path of the temporary files directory can be get within the C# application from environment variable TEMP.

Your batch file contains the lines:

@echo off
"Full\Name\Of\C#\ConsoleApplication.exe" "parameters for this application"
if exist "%TEMP%\SetYourVar.bat" (
   call "%TEMP%\SetYourVar.bat"
   del "%TEMP%\SetYourVar.bat" >nul
)
"Full\Name\Of\Other\DOS\Application.exe"

So instead of trying to set the environment variables to use in other applications directly within the C# console application, write the lines to set them into a batch file called next by the batch file which started the C# console application and next deletes this temporary batch file just used to set environment variables.

Mofi
  • 46,139
  • 17
  • 80
  • 143
0

Modify your C# program so it just display in the screen the value of the variable instead of set the variable itself. Then, execute the .exe from the Batch file this way:

for /F "delims=" %%a in ('abc.exe "any parameter"') do set datayyyymmdd=%%a
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Thanks for your comment.Do you mean that I just need to print my variable as console.writeline() and "for /F "delims=" %%a in ('abc.exe "any parameter"') do set datayyyymmdd=%%a" will give result by itself? – vish1990 Jun 23 '14 at 10:19
  • sir i tried but still,no result.Following is my sample code._:@echo off for /F "delims=" %%a in (C:\Users\u316383\Documents\Backup\ConsoleApplication3.exe) do set datayyyymmdd=%%a. ECHO datayyyymmdd. @wait 15:and simple code:using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication3 { class Program{ static void Main(string[] args) { Console.WriteLine("Hello World!"); } } } – vish1990 Jun 23 '14 at 13:48
  • You forgot the apostrophes in FOR /F command: `for /F "delims=" %%a in ('C:\Users\u316383\Documents\Backup\ConsoleApplication3.exe') do set datayyyymmdd=%%a`. The command to execute must be enclosed in apostrophes. – Aacini Jun 23 '14 at 19:39
  • Thanks for the reply.Now everything is working without arguements but with arguement,it again giving no output:@echo off FOR /F "usebackq delims=" %%a IN (`C:\Users\u316383\Documents\ReturnFileName.exe`) DO SET DATEVAR3000=%%a ECHO DATEVAR3000 is %DATEVAR3000% @Wait 15 but how to pass arguement in this? – vish1990 Jun 24 '14 at 15:05
  • Just place between the apostrophes _the exact same line_ you use in the command-line to execute the program. For example: `for /F "delims=" %%a in ('echo one "some " more') do set var=%%a` – Aacini Jun 24 '14 at 15:29
  • I Got it.complete code :@echo off FOR /F "usebackq delims=" %%a IN (`C:\Users\u316383\Documents\ReturnFileName.exe "C:\Users\u316383\Documents" "1232" "19901212"`) DO SET DATEVAR3000=%%a ECHO DATEVAR3000 is %DATEVAR3000% @Wait 15.Thanks all for help. – vish1990 Jun 24 '14 at 15:31
  • I appolozise for adding another problem.Now my code is working correctly but if exception comes in the program,DATEVAR3000 is only showing last few lines of exception and not all.Is there any limit of data that can be returned?/ – vish1990 Jun 25 '14 at 15:21