尝试从另一个应用程序启动应用程序继续崩溃

时间:2017-06-22 10:17:59

标签: c# process exec

我在C#中编写了一个程序,它从WPF接口获取文件夹路径,前缀和替换前缀。该程序的目的是在文件夹中搜索以前缀开头的所有文件,并使用新前缀重命名它们。这个程序没有任何问题。

现在我必须编写第二个必须调用前一个程序的程序,使用args []传递参数。我写了第二个程序,似乎所有的参数都正确地传递给了另一个(我查了一下),但是每次更换程序都会在几秒后耗尽(而在我的试用文件夹中正常运行几乎是立刻) 。我无法获得任何消息错误或异常,我只能获取报告崩溃的Windows警报消息。

以下是两个程序代码的重要部分......任何人都可以帮我找到问题吗?设置从一个程序传递到另一个程序是否有问题?非常感谢大家。

这可能是调用设置参数的问题吗?

替换前缀程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.IO;

namespace replace_prefix
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            string[] args;
            args = Environment.GetCommandLineArgs();
            if (args.Length > 1)
            {
                ((MainWindow)App.Current.MainWindow).Close();
                sf_textBox.Text = args[1];
                rp_textBox.Text = args[2];
                np_textBox.Text = args[2];
                replace();
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void ok_button_Click(object sender, RoutedEventArgs e)
        {
            replace();
        }

        private void replace() {
            if (sf_textBox.Text == "" || rp_textBox.Text == "" || np_textBox.Text == "")
                MessageBox.Show(this, "Insert all required parameters", "Parameter missing", MessageBoxButton.OK, MessageBoxImage.Error);
            else
            {
                using (StreamWriter w = File.CreateText("log.txt"))
                {

                    DirectoryInfo d = new DirectoryInfo(sf_textBox.Text);
                    FileInfo[] Files = d.GetFiles("*.*");
                    string filename, log;
                    foreach (FileInfo file in Files)
                    {
                        filename = file.Name;
                        int Place = filename.IndexOf(rp_textBox.Text);
                        if (Place == 0)
                        {
                            log = "file " + filename + " renamed ";
                            filename = filename.Remove(Place, rp_textBox.Text.Length).Insert(Place, np_textBox.Text);
                            file.MoveTo(file.Directory.FullName + "\\" + filename);
                            Log(log + filename, w);
                        }
                    }
                    w.Close();
                }
                Environment.Exit(0);
            }
        }

        public static void Log(string logMessage, TextWriter w)
        {
            w.Write(DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
            w.Write(" --> ");
            w.WriteLine(logMessage);
        }
    }
}

在其他程序中调用方法:

        public void LaunchCommandLineApp()
        {
            using (StreamWriter wl = File.CreateText(job.name + "_log.txt"))
            {
                lock (wl) wl.WriteLine("\\n" + DateTime.Now.ToString() + " --> " + job.name + ": " + job.process + " launched");
                // Use ProcessStartInfo class
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.CreateNoWindow = false;
                startInfo.UseShellExecute = false;
                startInfo.FileName = job.process;
                startInfo.WindowStyle = ProcessWindowStyle.Hidden;
                var count = job.args.Count(c => c == ';');
                startInfo.Arguments = "";// "-f ";
                while (count > 1)
                {
                    startInfo.Arguments += job.args.Substring(0, job.args.IndexOf(';', 0));
                    int i = job.args.IndexOf(';', 0) + 1, k = job.args.Length - 1;
                    job.args = job.args.Substring((job.args.IndexOf(';', 0) + 1), (job.args.Length - 1) - (job.args.IndexOf(';', 0)));
                    count--;
                }
                if (count == 1) {
                    int i = job.args.IndexOf(';', 0);
                    startInfo.Arguments += job.args.Substring(0, job.args.IndexOf(';', 0)); 

                }

                if (job.recurrency) job.updateRecurrency();

                try
                {
                    // Start the process with the info we specified.
                    // Call WaitForExit and then the using statement will close.
                    using (Process exeProcess = Process.Start(startInfo))
                    {
                        exeProcess.WaitForExit();
                        string tl;
                        try
                        {
                            tl = Path.GetDirectoryName(job.process);
                        }
                        catch (ArgumentException e) {
                            tl = null;
                        }
                        if (tl != null)
                        {
                            try
                            {
                                tl = tl + "\\log.txt";
                                StreamReader input = new StreamReader(tl);
                                tl = input.ReadLine();
                                while (tl != null)
                                {
                                    wl.WriteLine(tl);
                                    tl = input.ReadLine();
                                }
                                input.Close();
                            }
                            catch (Exception e) { }
                        }
                        lock (wl) wl.WriteLine(DateTime.Now.ToString() + " --> " + job.name + ": " + job.process + " successfully ended.");
                    }
                }
                catch (Exception e)
                {
                    //add local process log
                    lock (wl) wl.WriteLine(DateTime.Now.ToString() + " --> " + job.name + ": " + job.process + " failed to ended. " + e.Message.ToString());
                }

                wl.Close();
                InvokeExecutionFinished(new EventArgs());
            }

            //Send log via email
            //sendNotification();

        }

Crash report

0 个答案:

没有答案
相关问题