C# - 检查设备问题和系统问题

发布于 2024-08-08 06:40:21 字数 65 浏览 2 评论 0原文

在 C# 中,我如何检查设备和系统错误?使用 PowerShell Scipts 会很简单,还是会增加复杂性和难度?

In C#, How Could I go about checking for device and systems errors? Would it be simple to use PowerShell Scipts, or would that add to the complexity and difficulty?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜你已久 2024-08-15 06:40:21

对于 Windows 7 客户端,请查看Windows 故障排除平台。这是下载 上有更多详细信息。它使用 PowerShell 脚本来完成您所说的操作。此博客文章 展示了如何编写故障排除包 - 这非常简单。

我认为 WTP 不适用于下层平台。在这种情况下,我只需编写一些 PowerShell 脚本来检测并修复根本原因。如果您想将其包装在一个漂亮的 UI 中,请查看 PowerBoots - 创建 WPF 的简单方法GUI 位于脚本之上。如果您想在基于 C# 的 GUI 中托管 PowerShell,这非常简单。下面是来自 Forms 应用程序的代码片段:

    private void button1_Click(object sender, EventArgs e)
    {
        string cmd = @"Get-ChildItem $home\Documents -recurse | " +
                      "Where {!$_.PSIsContainer -and " +
                      "($_.LastWriteTime -gt (Get-Date).AddDays(-7))} | " +
                      "Sort Fullname | Foreach {$_.Fullname}";

        using (Runspace runspace = RunspaceFactory.CreateRunspace())
        {
            runspace.Open();
            using (Pipeline pipeline = runspace.CreatePipeline(cmd))
            {
                this.Cursor = Cursors.WaitCursor;

                pipeline.Commands.AddScript(cmd);
                Collection<PSObject> results = pipeline.Invoke();
                foreach (PSObject obj in results)
                {
                    listBox1.Items.Add(obj);
                }

                this.Cursor = Cursors.Default;
            }
        }
    }

您需要添加对 System.Management.Automation 程序集的引用。如果您已安装 Windows/.NET SDK,则该 SDK 应该位于 ProgramFiles\ReferenceAssemblies\Microsoft\WindowsPowerShell\v1.0 中。您还需要一些使用状态:

using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

For Windows 7 clients check out the Windows Troubleshooting Platform. Here is a download on it with more details. It uses PowerShell scripts to do exacty what you're talking about. This blog post shows how to author a troubleshooting pack - it's pretty easy.

I don't think WTP works on downlevel platforms. In this case, I would just write some PowerShell scripts to detect and fix root causes. If you want to wrap that up in a nice UI, check out PowerBoots - an easy way to create a WPF GUI on top of your script. If you want to host PowerShell in your on C#-based GUI it is very simple. Here's a code snippet from a Forms app:

    private void button1_Click(object sender, EventArgs e)
    {
        string cmd = @"Get-ChildItem $home\Documents -recurse | " +
                      "Where {!$_.PSIsContainer -and " +
                      "($_.LastWriteTime -gt (Get-Date).AddDays(-7))} | " +
                      "Sort Fullname | Foreach {$_.Fullname}";

        using (Runspace runspace = RunspaceFactory.CreateRunspace())
        {
            runspace.Open();
            using (Pipeline pipeline = runspace.CreatePipeline(cmd))
            {
                this.Cursor = Cursors.WaitCursor;

                pipeline.Commands.AddScript(cmd);
                Collection<PSObject> results = pipeline.Invoke();
                foreach (PSObject obj in results)
                {
                    listBox1.Items.Add(obj);
                }

                this.Cursor = Cursors.Default;
            }
        }
    }

You need to add a reference to the System.Management.Automation assembly. If you have installed the Windows/.NET SDK that should be in ProgramFiles\ReferenceAssemblies\Microsoft\WindowsPowerShell\v1.0. You will also need a couple of using statememets:

using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文