通过c#窗口应用程序启动.exe时出现意外错误

时间:2017-09-01 05:55:09

标签: c#

我开发了一个应用程序,它接受.exe文件并在命令提示符下运行,控制台输出在运行后以文本形式存储

如果.exe文件位于需要管理员权限的文件夹中,则生成空白文本文件

我尝试在管理模式下运行我的应用程序,但结果相同。

public void button2_Click(object sender, EventArgs e)
{
    string line="";
    string buffer="";
    string fileLine;
    string propertyName;
    int propertyNode = 0;
    //...................Delete if file exists..............................//
    // var appPathForText = Application.StartupPath + "\\PathForGtest.txt";
    // var appPathForXml = Application.StartupPath + "\\Text.xml";
    var appPathForText = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "PathForGtest.txt");
    var appPathForXml = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Text.xml");
    if (System.IO.File.Exists(appPathForText) || System.IO.File.Exists(appPathForXml))
    {
        System.IO.File.Delete(appPathForXml);
        System.IO.File.Delete(appPathForText);
    }
    //...................run in command prompt..............................//
    string strCmdText;
    strCmdText = "/C"+textBox1.Text + " --gtest_list_tests>>PathForGtest.txt";
    System.Diagnostics.Process.Start("CMD.exe", strCmdText);

    //........................................................................//

    //...................xml generator....................................... //
    XmlTextWriter writer = new XmlTextWriter("Text.xml", null);
    writer.WriteStartDocument();
    writer.WriteStartElement("VeracitySession");
    writer.WriteStartElement("TestCollection");
    Thread.Sleep(1000);

    string appPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),"PathForGtest.txt");
    // var appPath = Application.StartupPath+"\\PathForGtest.txt";

    System.IO.StreamReader file = new System.IO.StreamReader(appPath);
    //Console.WriteLine(h);


    while ((fileLine = file.ReadLine()) != null)
    {
        if ((fileLine.EndsWith(".")))
        {
            buffer = fileLine;
            continue;
        }
        else
        {
            line = fileLine.Trim();
            propertyNode = propertyNode + 1;
        }
        propertyName = buffer + line;

        writer.WriteStartElement("Test");
        writer.WriteAttributeString("ID", "{AB02A59E-2DF9-4EF4-AD3A-7B14BB3341BC}");
        writer.WriteAttributeString("StartTime", "");
        writer.WriteAttributeString("Description", "Runing Gtest through veracity");
        writer.WriteAttributeString("InstanceName", "RunGTest " + propertyNode);
        writer.WriteAttributeString("Result", "Failed");
        writer.WriteAttributeString("Enabled", "1");
        writer.WriteAttributeString("Checksum", "");
        writer.WriteAttributeString("EndTime", "");


        writer.WriteStartElement("PropertyCollection");
        writer.WriteStartElement("Property");
        writer.WriteAttributeString("Name", "External app path");
        writer.WriteAttributeString("Description", "Specify the path relative to Bin folder or you can specify the absolute path.May use env variable");
        writer.WriteString(textBox1.Text);
        writer.WriteEndElement();//property1

        writer.WriteStartElement("Property");
        writer.WriteAttributeString("Name", "Command line parameters");
        writer.WriteAttributeString("Description", "specify the command line parameter for Gtest");
        writer.WriteString("--gtest_filter=" + propertyName);
        writer.WriteEndElement();//property2

        writer.WriteStartElement("Property");
        writer.WriteAttributeString("Name", "Capture Std Out");
        writer.WriteAttributeString("Description", "Set this to true if the external app uses std out");
        writer.WriteString("1");
        writer.WriteEndElement();//property3

        writer.WriteStartElement("Property");
        writer.WriteAttributeString("Name", "File Name to Capture Std Out");
        writer.WriteAttributeString("Description", "Has effect only when property 'Capture Std Out'is set to true to Capture Std Out");
        writer.WriteString("Output.txt");
        writer.WriteEndElement();//property4

        writer.WriteStartElement("Property");
        writer.WriteAttributeString("Name", "Pass Phrase");
        writer.WriteAttributeString("Description", "Pass phrase to look for in the std out or the output file to determine if the test passed or failed");
        writer.WriteString("FAILED");
        writer.WriteEndElement();//property5

        writer.WriteEndElement();//propertyCollection

        writer.WriteEndElement();//Test
    }
    writer.WriteEndElement();//TestCollection
    writer.WriteEndDocument();//VeracitySession
    writer.Close();
    textBox1.Clear();
    Close();
}

0 个答案:

没有答案