c# - Process.Start(ProcessStartInfo) 不会将命令行发送到屏幕保护程序(.scr 文件)

标签 c# command-line-arguments screensaver

我目前正在编写一个应用程序来启动 Windows 10 上的屏幕保护程序并显示屏幕而不是黑色背景。这样 Bubbles 和亲戚就可以像在旧操作系统版本中一样工作。

这是我的完整代码:

using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

public class DrawOverMyScreen {
  public static void Main(string[] CommandLine) {
    switch (CommandLine[0]) {
      case "/c":
        DialogResult Answer = MessageBox.Show("What do you want to do?\n\n - Press \"Yes\" to configure the screensaver\n - Press \"No\" to change the screensaver\n - Press \"Cancel\" to do nothing", "DrawOverMyScreen Configuration", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3);
        switch (Answer) {
          case DialogResult.Yes:
            Screensaver("/c");
            break;

          case DialogResult.No:
            throw new NotImplementedException();
            break;

          default:
            break;
        }
        break;

      default:
        Screensaver("/s");
        break;
    }
  }

  public static void Screensaver(string CommandLine) {
    RegistryKey Settings = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\DrawOverMyScreen");
    if (Settings != null) {
      string ScreensaverLocation = Settings.GetValue("Screensaver", string.Empty).ToString();
      if (!string.IsNullOrEmpty(ScreensaverLocation) && File.Exists(ScreensaverLocation)) {
        Process Screensaver = Process.Start(new ProcessStartInfo(ScreensaverLocation, CommandLine));
        Screensaver.WaitForExit();
      }
    }
  }
}

注意 Screensaver 方法。它使用 Process.Start(new ProcessStartInfo(ScreensaverLocation, CommandLine)); 启动屏幕保护程序。但是每当我执行 Screensaver("/c"); 来运行屏幕保护程序的配置实用程序时,我只会得到正常的屏幕保护程序 View (在一定时间后闲置时得到的 View )。使用这样的运行提示符:C:\Windows\SysWOW64\SCREEN~1.SCR/c 也给出了相同的结果,但命令行提示符实际上打开了配置实用程序。

为什么它不起作用,我怎样才能让它起作用?

最佳答案

仅根据您提供的内容,我无法告诉您为什么它不起作用。我没有屏幕保护程序来测试它(据我所知)。但是我可以用记事本打开一个文本文件来完成所有这四个操作:

单独的 ProcessStartInfo

     ProcessStartInfo procInfo = new ProcessStartInfo("notepad.exe", "c:\\test.txt");
     Process proc = Process.Start(procInfo);
     proc.WaitForExit();

将 ProcessStartInfo 与属性分开

     ProcessStartInfo procInfo = new ProcessStartInfo();
     procInfo.Arguments = "c:\\test.txt";
     procInfo.FileName = "notepad.exe";
     Process proc = Process.Start(procInfo);
     proc.WaitForExit();

内联 ProcessStartInfo

     Process proc = Process.Start(new ProcessStartInfo("notepad.exe", "c:\\test.txt"));
     proc.WaitForExit();

没有 PSI,只有过程

     Process proc = Process.Start("notepad.exe", "c:\\test.txt");
     proc.WaitForExit();

您可能希望使用第一个,这样您就可以在“Process proc...”行上设置断点并检查 procInfo 的属性。 Arguments 属性应该显示第二个值(在我的例子中,c:\\test.txt),FileName 属性应该是您正在执行的路径(我的是 notepad.exe)。

编辑:我添加了带有属性的单独的一个,这样您就可以真正看到明确的设置。

配置屏保

我已经使用 3D 文本屏幕保护程序编写了一个示例:

     string scrPath = @"C:\Windows\System32\ssText3d.scr";
     ProcessStartInfo procInfo = new ProcessStartInfo();
     procInfo.FileName = scrPath;
     procInfo.Verb = "config";
     procInfo.UseShellExecute = false;
     Process proc = Process.Start(procInfo);
     proc.WaitForExit();

我没有使用Arguments。相反,我使用了 Verb。这需要将 UseShellExecute 设置为 false。我得到了预期的配置对话框,而不是正在运行的屏幕保护程序。

关于动词的更多信息

这里是屏幕保护程序的动词。 enter image description here

您还可以定义自定义动词:Register an Application to Handle Arbitrary File Types

关于c# - Process.Start(ProcessStartInfo) 不会将命令行发送到屏幕保护程序(.scr 文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36754247/

相关文章:

c# - 如何以编程方式分析以了解两个表之间存在多对多关系?

haskell - 当提供多个互斥选项时,如何使用 optparse-applicative 提供特定的错误消息?

java - 如何解析 Java 中的命令行参数?

xcode - 您不能在此版本的 OS X 上使用屏幕保护程序

windows - 如何判断屏幕保护程序正在运行?

java - 创建另一个进程的 HWND 的子窗口? (例如屏保预览)

c# - 异步 lambda 中的参数

c# - Box C# 对象作为通用接口(interface)

c# - 找不到类型或命名空间名称 'ICloudRecoEventHandler'

python argparse如何将整个命令作为字符串获取