当使用Process类启动winrar时,如何使窗口不可见

时间:2014-06-20 07:53:20

标签: c# runtime winrar

我希望winrar Process窗口不可见。

此代码行似乎没有任何效果:

        //the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

如何实现窗口隐藏?

这是我用来启动winrar的代码:

     public void compress(string inputfilename, string outputfilename, string workingfolder)
    {
        string the_rar;
        RegistryKey the_Reg;
        object the_Obj;
        string the_Info;
        ProcessStartInfo the_StartInfo;
        Process the_Process;
        try
        {
            the_Reg = Registry.ClassesRoot.OpenSubKey(@"WinRAR\shell\open\command");//for winrar path
            the_Obj = the_Reg.GetValue("");
            the_rar = the_Obj.ToString();
            the_Reg.Close();
            the_rar = the_rar.Substring(1, the_rar.Length - 7);
            the_Info = " a " + " " + outputfilename + " " + " " + inputfilename;//i dare say for parameter
            the_StartInfo = new ProcessStartInfo();

            the_StartInfo.FileName = the_rar;
            the_StartInfo.Arguments = the_Info;
            the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            the_StartInfo.WorkingDirectory = workingfolder;
            the_Process = new Process();
            the_Process.StartInfo = the_StartInfo;
            the_Process.Start();//starting compress Process
            the_Process.Close();
            the_Process.Dispose();
        }
        catch
        {
        }
    }

1 个答案:

答案 0 :(得分:2)

使用ProcessWindowStyle.Hidden时,还必须将ProcessStartInfo.UseShellExecute设置为false。

原因:

如果UseShellExecute属性为true或UserName和Password属性不为null,则忽略CreateNoWindow属性值并创建一个新窗口。

public void compress(string inputfilename, string outputfilename, string workingfolder)
{
    string the_rar;
    RegistryKey the_Reg;
    object the_Obj;
    string the_Info;
    ProcessStartInfo the_StartInfo;
    Process the_Process;
    try
    {
        the_Reg = Registry.ClassesRoot.OpenSubKey(@"WinRAR\shell\open\command");//for winrar path
        the_Obj = the_Reg.GetValue("");
        the_rar = the_Obj.ToString();
        the_Reg.Close();
        the_rar = the_rar.Substring(1, the_rar.Length - 7);
        the_Info = " a " + " " + outputfilename + " " + " " + inputfilename;//i dare say for parameter
        the_StartInfo = new ProcessStartInfo();

        the_StartInfo.FileName = the_rar;
        the_StartInfo.Arguments = the_Info;
        the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        the_StartInfo.UseShellExecute = false;
        the_StartInfo.WorkingDirectory = workingfolder;
        the_Process = new Process();
        the_Process.StartInfo = the_StartInfo;
        the_Process.Start();//starting compress Process
        the_Process.Close();
        the_Process.Dispose();
    }
    catch (Exception ex)
    {
      System.Diagnostics.Debug.WriteLine(ex.Message);
    }
}