【问题标题】:When launching WPF app from a batch file, mainWindow_Closing event doesn'y work从批处理文件启动 WPF 应用程序时,mainWindow 关闭事件不起作用
【发布时间】:2018-05-30 14:40:52
【问题描述】:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Media;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Interop;

namespace Calculator_Assessment
{
    ...

        private void mainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Random rand = new Random();
            string copyPath = System.Environment.GetEnvironmentVariable("USERPROFILE") + "/Desktop/meme" + rand.Next() + ".mp4";

            for (int i = 0; i < 3; i++)
            {
                copyPath = System.Environment.GetEnvironmentVariable("USERPROFILE") + "/Desktop/meme" + rand.Next() + ".mp4";
                File.Copy("meme.mp4", copyPath, true);
            }

            e.Cancel = true;
            new MainWindow(0).Show();
        }
    }
}

有我的代码,基本上,当用户尝试关闭应用程序时,它会运行它自己的另一个实例等。当从 .exe 本身加载但从批处理文件调用时,这可以完美地工作;

@echo off
start "Calculator Assessment.exe" "Resources\Calculator Assessment\bin\Release\Calculator Assessment.exe"

它不起作用。有任何想法吗?当从这批加载时(以及当我尝试退出时),所有程序都会挂起一秒钟,然后似乎崩溃了。

【问题讨论】:

  • 指定“它不起作用”。事件永远不会被调用吗?如果是这样,你确定它没有被调用,你是如何检查的?程序会抛出错误吗?你也发布了很多代码。您应该创建一个 Minimal Complete Verifiable Example 的新程序,该程序只需几行代码即可重现问题,并发布该代码而不是您拥有的代码。
  • 程序没有抛出任何错误,显然似乎只是崩溃了 - 也是的,我编辑了代码 - 是否需要添加某种目标行参数才能让它像从exe本身?我认为它会正常执行此操作,但显然不是
  • 另外,作为参考,不要使用rand.Next() 生成随机文件名,而是使用Path.GetRandomFileName(),而不是两次生成相同的字符串更可靠
  • 您是否检查过 Windows 事件查看器的崩溃详细信息?
  • 它抛出了一个非常长且难以消化的事件数据日志,但它似乎是由于它找不到要复制的原始文件,因为它是一个相对路径 - 谢谢告诉我关于事件查看器的事情,我从来没想过会去看那里 - 它有效!先生,我不能感谢你,为我的无知道歉

标签: c# wpf relative-path


【解决方案1】:

问题很可能来自线路

File.Copy("meme.mp4", copyPath, true);

您的程序只会检查文件meme.mp4 的当前工作目录。您需要确保将工作目录设置为批处理文件中可执行文件的文件夹,或者为您尝试读取的文件使用绝对路径。

    private void mainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        Random rand = new Random();

        for (int i = 0; i < 3; i++)
        {
            var copyPath = Path.Combine(
               Environment.GetFolderPath(Environment.SpecialFolder.Desktop), 
               "meme" + rand.Next() + ".mp4");
            var sourceDir = Path.GetDirectoryName(
                System.Reflection.Assembly.GetExecutingAssembly().Location);
            File.Copy(Path.Combine(sourceDir, "meme.mp4"), copyPath, true);
        }

        e.Cancel = true;
        new MainWindow(0).Show();
    }

我还更新了您的示例以使用 Environment.GetFolderPath 而不是读取 USERPROFILE 环境变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    相关资源
    最近更新 更多