I am developing a C# program to mask the user password. I am successful with that. I want the set the same user password as varible to the batch script( parent process ) from where the C# EXE is triggered. any help is greatly appreciated!!!
I also like to understand if there is any better approach to pass the value from C# to the parent process( *.bat file).
The idea here is ...
Batch Script
C# program ( mask the password / set the password as variable to the Batch script)
Batch use that variable pass that as argument to the PLM tool .
------------------------------------------------------------------------------------
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
using System.Collections.Specialized;
using System.Diagnostics;
namespace ConsoleApplicationUtil
{
class Program
{
static int Main(string[] args)
{
int PLM_RETURN = 0;
string password = "";
for (int iNx = 0; iNx < args.Length; iNx++)
{
Console.WriteLine("args[ {0} ]::< {1} >", iNx,args[iNx]);
}
Console.Write("Enter your password: ");
ConsoleKeyInfo key;
do
{
key = Console.ReadKey(true);
// Backspace Should Not Work
if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
{
password += key.KeyChar;
Console.Write("*");
}
else
{
if (key.Key == ConsoleKey.Backspace && password.Length > 0)
{
password = password.Substring(0, (password.Length - 1));
Console.Write("\b \b");
}
}
}
// Stops Receving Keys Once Enter is Pressed
while (key.Key != ConsoleKey.Enter);
Console.WriteLine();
Console.WriteLine("The Password You entered is : " + password);
// Environment.SetEnvironmentVariable(args[3], password, EnvironmentVariableTarget.User);
int ParentPID = Process.GetProcessById(Process.GetCurrentProcess().Id).Parent().Id;
foreach (Process proc in Process.GetProcesses())
{
Console.WriteLine("Checking Process: " + proc.ProcessName + ":" + proc.Id);
//StringDictionary sd = proc.StartInfo.EnvironmentVariables;
//if (proc.ProcessName.Equals("cmd"))
if (proc.Id.Equals(ParentPID) && proc.ProcessName.Equals("cmd"))
{
string s1 = proc.StartInfo.Arguments;
StringDictionary env_val = proc.StartInfo.EnvironmentVariables;
Console.WriteLine("================================================");
if (env_val.ContainsKey("PASS_APP"))
{
Console.WriteLine("FOUND IT!!!");
env_val.Add(args[3], password);
//proc.StartInfo.Arguments.IndexOf(password);
}
else
{
Console.WriteLine("NOOOOOOOOOOOOOO!!!");
}
Console.WriteLine("================================================");
}
}
return PLM_RETURN;
}
}
}