从批处理文件启动 WPF 应用程序时,mainWindow_Closing 事件不起作用
When launching WPF app from a batch file, mainWindow_Closing event doesn'y work
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"
没用。有任何想法吗?从这批加载时(以及当我尝试退出时),所有程序都会挂起一秒钟然后似乎崩溃。
喜欢的问题来自行
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
环境变量。
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"
没用。有任何想法吗?从这批加载时(以及当我尝试退出时),所有程序都会挂起一秒钟然后似乎崩溃。
喜欢的问题来自行
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
环境变量。