Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试在一次单击按钮中完成多个操作_C#_Wpf_Visual Studio - Fatal编程技术网

C# 尝试在一次单击按钮中完成多个操作

C# 尝试在一次单击按钮中完成多个操作,c#,wpf,visual-studio,C#,Wpf,Visual Studio,我一直在使用Stackoverflow来构建这个应用程序,一切都进行得很顺利,感谢所有有用的帖子 ErrorFixClick是我被卡住的一个按钮。我想在单击一个按钮时执行多个操作 这是我拼凑的代码。它是不是很漂亮的代码,不。当然,它能写得更好吗!它有用吗?当作为一个单独的按钮事件运行时,可以执行一些操作,但是组合起来就不起作用了 我已经十年没有上过C#类课程了,我需要一些帮助。我已经为每个操作的代码添加了一些注释,使其更易于理解 提前谢谢 using System; using System.C

我一直在使用Stackoverflow来构建这个应用程序,一切都进行得很顺利,感谢所有有用的帖子

ErrorFixClick是我被卡住的一个按钮。我想在单击一个按钮时执行多个操作

这是我拼凑的代码。它是不是很漂亮的代码,不。当然,它能写得更好吗!它有用吗?当作为一个单独的按钮事件运行时,可以执行一些操作,但是组合起来就不起作用了

我已经十年没有上过C#类课程了,我需要一些帮助。我已经为每个操作的代码添加了一些注释,使其更易于理解

提前谢谢

using System;
using System.Collections.Generic;
using System.Diagnostics;
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.ServiceProcess;
using System.IO;

namespace SuperDupperDesktopApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();


    }

    private void StartME_Click(object sender, RoutedEventArgs e)
    {
        Process.Start("C:\\Program Files\\OSE\\StartME.exe");
    }
    private void Reboot_Click(object sender, RoutedEventArgs e)
    {
        Process.Start("shutdown.exe", "-r -t 05 -f");
    }
    private void Info_Click(object sender, RoutedEventArgs e)
    {
        string hostname = System.Environment.MachineName;

        MessageBox.Show(Environment.OSVersion.ToString());
        MessageBox.Show(hostname);

    }


    private void ErrorFix_Click(object sender, RoutedEventArgs e) // <-- This is broken.
    {

        /// Stop a service
        Process cmd = new Process();
        cmd.StartInfo.FileName = @"cmd.exe";
        cmd.StartInfo.Arguments = @"/c SC STOP COMSVCS"; 
        cmd.Start();
        cmd.WaitForExit();

        ///Delete a file
        if (File.Exists(@"C:\op\cp.dat"))
        {
            File.Delete(@"C:\op\cp.dat");
        }

        /// Kill the App
        foreach (var process in Process.GetProcessesByName("StartME"))
        {
            process.Kill();
        }

        /// Reboot the PC
        Process.Start("shutdown.exe", "-r -t 01 -f");

    }
}
使用系统;
使用System.Collections.Generic;
使用系统诊断;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用System.ServiceProcess;
使用System.IO;
命名空间SuperDupperDesktopApp
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
私有无效开始单击(对象发送者,路由目标)
{
Process.Start(“C:\\Program Files\\OSE\\StartME.exe”);
}
私有无效重新启动\u单击(对象发送方,路由目标)
{
进程启动(“shutdown.exe”,“-r-t05-f”);
}
私有无效信息\u单击(对象发送者,路由目标)
{
字符串hostname=System.Environment.MachineName;
Show(Environment.OSVersion.ToString());
Show(主机名);
}

private void error fix_Click(对象发送方,RoutedEventArgs e)/我建议将服务停止切换为使用库函数,而不是启动另一个进程:


这里不工作意味着什么,出现任何异常还是什么?当我运行应用程序并按下ErrorFix#u Click按钮事件时,没有错误,也没有操作。Microsoft(R)Roslyn C#Compiler version 2.1.0.61520正在从“CSharpInteractive.rsp”加载上下文。键入“#帮助”以获取更多信息。>单击private void ErrorFix#(object sender,RoutedEventArgs e)//使用ErrorFix\u Click-如果我注释掉停止服务代码并以交互模式运行代码的其余部分,它就会工作。运行编译后的代码时,不会运行停止服务代码。学习使用调试器