如何使用Processstartinfo在C#中使用现有加密批处理文件加密文件?

时间:2015-04-24 05:05:44

标签: batch-file c#-4.0 encryption console-application

有人可以帮我解决如何使用ProcessStartInfo类在c#中使用现有加密脚本批处理文件加密文件吗?

我是文件加密的新手。我正在传递一个参数路径,即我需要加密的文本文件的路径。

这是我的加密脚本批处理文件的路径

      D:\CBMS-Keenan\core\Transmittals\Scripts\123gpg.bat|gpg||false

这是我现有的代码

        public string PerformAction(string path)// Path input file name path(.txt)
        {
        //TransmittalBatch batch = new TransmittalBatch();
        string _transmitfile=string.Empty;

        string text1 = "";

        string batchfilename=Path.GetFileName(path);
        string renamedfile = "";
        if((this._filename!=null)&& (this._filename.Length > 0))
        {
            renamedfile = this._filename;
        }
        else
        {
            if(this._replacesuffix)
            {
      renamedfile =   batchfilename.Remove(batchfilename.LastIndexOf("."));

            }
            else
            {
                renamedfile = batchfilename;
            }
            if((this._suffix!=null)&&(this._suffix.Length>0))
            {
                renamedfile = renamedfile + "." + this._suffix.Replace(".", "");
            }
        }
        if ((batchfilename != null) && (batchfilename.Length > 0))
        {
            ProcessStartInfo _info = new ProcessStartInfo(this._scriptfile, "\"" + batchfilename + "\"\"" + renamedfile + "\"");//this._scriptfile is encryprtion batch file //batchfilename is input batch file which we got from the path//renamed file is the filerenamed after the encryption
            _info.CreateNoWindow = true;
            _info.RedirectStandardOutput = true;
            _info.RedirectStandardInput = true;
            _info.RedirectStandardError = true;
            _info.UseShellExecute = false;

            Process _process = Process.Start(_info);
            StreamReader _streamreader1 = _process.StandardOutput;
            StreamReader _streamreader2 = _process.StandardError;
            _process.WaitForExit(300000);
            if(_process.HasExited)
            {
                text1 = _streamreader1.ReadToEnd();
                text1 = text1 + "Errors:\r\n";
                text1 = text1 + _streamreader2.ReadToEnd();
            }
            //batch.RuleActionResults["LatestWorkingFilename"] = text3;
            _process.StandardInput.WriteLine(text1);
            _transmitfile = renamedfile;

        }
        else
        {
            text1 = "No files to encrypt!";
        }
        //batch.RuleActionResults["Encryption Results"] = text1;
        string _ecnryptedfile = text1;
        //Console.WriteLine("Encrypted result:" + _ecnryptedfile);

        return _transmitfile;
        //return batch.RuleActionResults["EncryptionResults"].ToString();
    }

0 个答案:

没有答案