加载Console Application的应用程序

时间:2014-04-14 18:36:13

标签: c# wpf exe

我在C#和WPF应用程序中有一个应用程序,它调用执行某些任务的控制台应用程序并将调用作为主窗体。第一步已完成,但在尝试回调主应用程序时,则无法实现。

这是调用控制台应用程序的主要应用程序方法

private void Window_Loaded(object sender, RoutedEventArgs e)
    {

        var doc = new XmlDocument();
        doc.Load("http://www.mysite.org.br/Cantina/arquivoXML.xml");
            if (doc.DocumentElement != null)
            {
                var node = doc.DocumentElement.SelectSingleNode("/Application/Version");
                var node1 = doc.DocumentElement.SelectSingleNode("/Application/ZipFile");
                if (node != null)
                {
                    var version = node.InnerText;
                    var versionAssembly = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
                    if (Convert.ToDouble(version) > Convert.ToDouble(versionAssembly))
                    {
                        Process.Start("Updater.exe");
                    }
                }
            }
       }

加载控制台应用程序的Updater.exe执行其任务,承载IASD.ASCS.WPF.exe,并自动关闭,如下面的代码所示。

 using System.Diagnostics;
 using Ionic.Zip;
 using System.Xml;
 using System.IO;


namespace Updater
{
    public class Program
    {
        private static void Main()
        {
           Process[] process = Process.GetProcessesByName("IASD.ASCS.WPF");
            foreach (Process proc in process)
        {
            if (!proc.HasExited)
                proc.Kill();
        }
        XmlDocument doc = new XmlDocument();
        doc.Load("http://www.mysite.org.br/Cantina/arquivoXML.xml");
        if (doc.DocumentElement != null)
        {
            XmlNode node1 = doc.DocumentElement.SelectSingleNode("/Application/ZipFile");
            if (node1 != null)
            {
                string zipfile = node1.InnerText;
                const string end = ("http://www.mysite.org.br/Cantina/");
                string file = (end + zipfile);
                string path = System.AppDomain.CurrentDomain.BaseDirectory;
                string path2 = (System.AppDomain.CurrentDomain.BaseDirectory + @"\Temp");
                string path3 = System.IO.Path.Combine(typeof(Program).Assembly.Location, "IASD.ASCS.WPF.exe");
                ZipFile zipFile = ZipFile.Read(file);
                {
                    foreach (var zipEntry in zipFile)
                    {
                        zipEntry.Extract(path2,ExtractExistingFileAction.OverwriteSilently);
                    }
                }
                string dirTemp = path2;
                string dirInstalacao = path;
                string[] arquivos = Directory.GetFiles(path2);
                foreach (string item in arquivos)
                {
                    string nomedoarquivo = Path.GetFileName(item);
                    if (nomedoarquivo != null)
                    {
                        string destino = Path.Combine(dirInstalacao, nomedoarquivo);
                        File.Copy(item, destino, true);
                    }
                }
                string[] arquivosApagar = Directory.GetFiles(path2);
                foreach (string item in arquivosApagar)
                {
                    File.Delete(item);
                }
                Process.Start(path3);
            }
        }
        const string nomeExecutavel2 = "Updater.exe";
        foreach (Process pr2 in Process.GetProcessesByName(nomeExecutavel2))
        {
            if (!pr2.HasExited) pr2.Kill();
        }
    }
  }
}

但没有用。

可执行文件未调用

现在如果我将路径标记为fucniona下面的更新程序代码。

using System.Diagnostics;
using Ionic.Zip;
using System.Xml;
using System.IO;


namespace Updater
{
 public class Program
{
    private static void Main()
    {
       Process[] process = Process.GetProcessesByName("IASD.ASCS.WPF");
        foreach (Process proc in process)
        {
            if (!proc.HasExited)
                proc.Kill();
        }
        XmlDocument doc = new XmlDocument();
        doc.Load("http://www.escolasaps.org.br/Cantina/arquivoXML.xml");
        if (doc.DocumentElement != null)
        {
            XmlNode node1 =      doc.DocumentElement.SelectSingleNode("/Application/ZipFile");
            if (node1 != null)
            {
                string zipfile = node1.InnerText;
                const string end = ("http://www.escolasaps.org.br/Cantina/");
                string file = (end + zipfile);
                ZipFile zipFile = ZipFile.Read(file);
                {
                    foreach (var zipEntry in zipFile)
                    {
                        zipEntry.Extract(@"c:\IASD\CantinaEscolar\Temp",ExtractExistingFileAction.OverwriteSilently);
                    }
                }
                string dirTemp = @"c:\IASD\CantinaEscolar\Temp";
                string dirInstalacao = @"c:\IASD\CantinaEscolar\";
                string[] arquivos = Directory.GetFiles(dirTemp);
                foreach (string item in arquivos)
                {
                    string nomedoarquivo = Path.GetFileName(item);
                    if (nomedoarquivo != null)
                    {
                        string destino = Path.Combine(dirInstalacao, nomedoarquivo);
                        File.Copy(item, destino, true);
                    }
                }
                string[] arquivosApagar = Directory.GetFiles(dirTemp);
                foreach (string item in arquivosApagar)
                {
                    File.Delete(item);
                }
                Process.Start("IASD.ASCS.WPF.exe");
            }
        }
        const string nomeExecutavel2 = "Updater.exe";
        foreach (Process pr2 in Process.GetProcessesByName(nomeExecutavel2))
        {
            if (!pr2.HasExited) pr2.Kill();
        }
      }
   }
}

1 个答案:

答案 0 :(得分:1)

试试这个:

string path = System.IO.Path.Combine(typeof(Program).Assembly.Location, "MyExe.exe");
using (Process.Start(path))
{
}