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

C# 尝试从另一个应用程序启动应用程序继续崩溃,c#,process,exec,C#,Process,Exec,我用C语言编写了一个程序,它从WPF接口获取文件夹路径、前缀和替换前缀。该程序的目的是在文件夹中搜索以前缀开头的所有文件,并使用新前缀重命名它们。这个程序没有任何问题 现在我必须编写第二个程序,该程序必须调用前一个程序,使用args[]传递参数。我编写了第二个程序,似乎所有参数都正确地传递给了另一个(我检查了它),但每次更换程序都会在几秒钟后运行(而我的试用文件夹中的正常运行几乎是即时的)。我不能有任何消息错误或异常,我只能获取报告崩溃的windows警报消息 这是两个程序代码的重要部分。。。有

我用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);
        }
    }
}

我只能获取报告崩溃的windows警报消息。
你能与我们分享吗?我在代码末尾添加了一个链接,很难从你的代码中判断出什么是错误的,但是当你将args放入文本框中时,注意到你在WPF应用程序中使用了args[2]两次,这是问题吗?哦,是的,这是个错误,但不,我相信这不是问题所在。(该错误仅设置了研究前缀与替换前缀之间的匹配)。我知道这很难。我的问题就是这样。我无法获得足够的信息来知道问题可能出在哪里……如果我是你,我会将进行重命名的代码(即替换方法)移动到类库中,然后从WPF应用程序和第二个程序调用该代码。然后可以调试等。
        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();

        }