2013-12-11 186 views
2

我正在开发一个C#程序来屏蔽用户密码。我很成功。我希望设置与C#EXE触发的批处理脚本(父进程)相同的用户密码。任何帮助是极大的赞赏!!!如何为cmd进程设置环境变量? - 不是系统环境变量

我也想知道是否有更好的方法将值从C#传递给父进程(* .bat文件)。

这里的想法是......

  1. 批处理脚本

  2. C#程序(屏蔽密码/设置密码作为变量的批处理脚本)

  3. 批量使用的是作为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; 
     } 
    } 
} 
+0

为什么批处理文件涉及到第一位?你在批处理文件中做什么,你不能在你的C#程序中做什么? –

回答

2

您可以不要修改另一个进程的环境变量(至少不是没有某种调试特权或者某种愚蠢的东西)。修改另一个进程的环境变量需要修改该进程的environment block,即基本上修改该进程的内存。

将信息从子进程传递回其父进程的正常方式是使用标准输出(即Console.WriteLine),尽管批处理文件使其对assign that output to a variable有点痛苦,因此可以使用它。

如果你想写一个接受屏蔽文本输入的批处理文件,那么你可以使用一些sneaky VBScript magic

一个完全不同的方法解决问题的方法是不要试图将密码传递回批处理文件,而只是让C#应用程序自己使用密码(即执行批处理文件要做的事情,可能只是通过调用另一个批处理文件作为参数传递密码)。

0

我建议如果你想操纵控制台应用程序产生的变量,为什么不在操作系统应用程序本身中操作它们,而不是经历所有的麻烦将变量返回给调用者?

AFAIK唯一的简单的方法来“回报”的变量是简单地在入口点的最后返回一个整数,例如,

static int Main(string[] args) 
{ 
    return 912; 
} 

的ERRORLEVEL变量将被设置为值,你返回。它可以如下

test.exe 
echo %errorlevel% 

可惜你不能返回一个字符串,这样一来,要做到这一点,你必须做什么贾斯汀建议进行访问。