我正在开发一个C#程序来屏蔽用户密码。我很成功。我希望设置与C#EXE触发的批处理脚本(父进程)相同的用户密码。任何帮助是极大的赞赏!!!如何为cmd进程设置环境变量? - 不是系统环境变量
我也想知道是否有更好的方法将值从C#传递给父进程(* .bat文件)。
这里的想法是......
批处理脚本
C#程序(屏蔽密码/设置密码作为变量的批处理脚本)
批量使用的是作为PLM工具参数的变量传递。
------------------------------------------- -----------------------------------------
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;
}
}
}
为什么批处理文件涉及到第一位?你在批处理文件中做什么,你不能在你的C#程序中做什么? –