获取c#中与ProcessStartInfo一起使用的可用动词列表(文件关联)

本文关键字:列表 文件 关联 中与 ProcessStartInfo 一起 获取 | 更新日期: 2023-09-27 18:06:12

我正在尝试使用ProcessStartInfo类打开和打印文件。(文件可以是任何文件,但我们假设它是PDF文件)

  ProcessStartInfo pi = new ProcessStartInfo(file);
  pi.Arguments = Path.GetFileName(file);
  pi.WorkingDirectory = Path.GetDirectoryName(file);
  pi.Verb = "OPEN";  
  Process.Start(pi);

这对pi.Verb = "OPEN";很有效。一些应用程序也使用动词"PRINT"来注册自己,但只有一些应用程序这样做。在我的情况下(Windows PDF查看器),我得到一个异常时,试图执行pi.Verb = "PRINT";

是否有一种方法可以在运行时查看c#中特定类型可用的所有动词?

Thx a lot

获取c#中与ProcessStartInfo一起使用的可用动词列表(文件关联)

ProcessStartInfo.Verbs属性有点坏,因为它不考虑新版本的Windows (Windows 8及以上)检索注册应用程序的方式。该属性只检查为在HKCR'.ext下定义的ProgId注册的动词(可以在参考源中看到),并且不考虑其他地方,例如在注册表键HKEY_CURRENT_USER'SOFTWARE'Microsoft'Windows'CurrentVersion'Explorer'FileExts'.ext或其他一些地方,例如通过策略定义。

获取已注册的动词

最好的方法是不依赖于直接检查注册表(如ProcessStartInfo类所做的),而是使用适当的Windows API函数AssocQueryString来检索相关的ProgId:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Win32;
class Program
{
    private static void Main(string[] args)
    {
        string fileName = @"E:'Pictures'Sample.jpg";
        string progId = AssocQueryString(AssocStr.ASSOCSTR_PROGID, fileName);
        var verbs = GetVerbsByProgId(progId);
        if (!verbs.Contains("printto"))
        {
            throw new Exception("PrintTo is not supported!");
        }
    }
    private static string[] GetVerbsByProgId(string progId)
    {
        var verbs = new List<string>();
        if (!string.IsNullOrEmpty(progId))
        {
            using (var key = Registry.ClassesRoot.OpenSubKey(progId + "''shell"))
            {
                if (key != null)
                {
                    var names = key.GetSubKeyNames();
                    verbs.AddRange(
                        names.Where(
                            name => 
                                string.Compare(
                                    name, 
                                    "new", 
                                    StringComparison.OrdinalIgnoreCase) 
                                != 0));
                }
            }
        }
        return verbs.ToArray();
    }
    private static string AssocQueryString(AssocStr association, string extension)
    {
        uint length = 0;
        uint ret = AssocQueryString(
            AssocF.ASSOCF_NONE, association, extension, "printto", null, ref length);
        if (ret != 1) //expected S_FALSE
        {
            throw new Win32Exception();
        }
        var sb = new StringBuilder((int)length);
        ret = AssocQueryString(
            AssocF.ASSOCF_NONE, association, extension, null, sb, ref length);
        if (ret != 0) //expected S_OK
        {
            throw new Win32Exception();
        }
        return sb.ToString();
    }
    [DllImport("Shlwapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern uint AssocQueryString(
        AssocF flags,
        AssocStr str,
        string pszAssoc,
        string pszExtra,
        [Out] StringBuilder pszOut,
        ref uint pcchOut);
    [Flags]
    private enum AssocF : uint
    {
        ASSOCF_NONE = 0x00000000,
        ASSOCF_INIT_NOREMAPCLSID = 0x00000001,
        ASSOCF_INIT_BYEXENAME = 0x00000002,
        ASSOCF_OPEN_BYEXENAME = 0x00000002,
        ASSOCF_INIT_DEFAULTTOSTAR = 0x00000004,
        ASSOCF_INIT_DEFAULTTOFOLDER = 0x00000008,
        ASSOCF_NOUSERSETTINGS = 0x00000010,
        ASSOCF_NOTRUNCATE = 0x00000020,
        ASSOCF_VERIFY = 0x00000040,
        ASSOCF_REMAPRUNDLL = 0x00000080,
        ASSOCF_NOFIXUPS = 0x00000100,
        ASSOCF_IGNOREBASECLASS = 0x00000200,
        ASSOCF_INIT_IGNOREUNKNOWN = 0x00000400,
        ASSOCF_INIT_FIXED_PROGID = 0x00000800,
        ASSOCF_IS_PROTOCOL = 0x00001000,
        ASSOCF_INIT_FOR_FILE = 0x00002000
    }
    private enum AssocStr
    {
        ASSOCSTR_COMMAND = 1,
        ASSOCSTR_EXECUTABLE,
        ASSOCSTR_FRIENDLYDOCNAME,
        ASSOCSTR_FRIENDLYAPPNAME,
        ASSOCSTR_NOOPEN,
        ASSOCSTR_SHELLNEWVALUE,
        ASSOCSTR_DDECOMMAND,
        ASSOCSTR_DDEIFEXEC,
        ASSOCSTR_DDEAPPLICATION,
        ASSOCSTR_DDETOPIC,
        ASSOCSTR_INFOTIP,
        ASSOCSTR_QUICKTIP,
        ASSOCSTR_TILEINFO,
        ASSOCSTR_CONTENTTYPE,
        ASSOCSTR_DEFAULTICON,
        ASSOCSTR_SHELLEXTENSION,
        ASSOCSTR_DROPTARGET,
        ASSOCSTR_DELEGATEEXECUTE,
        ASSOCSTR_SUPPORTED_URI_PROTOCOLS,
        ASSOCSTR_PROGID,
        ASSOCSTR_APPID,
        ASSOCSTR_APPPUBLISHER,
        ASSOCSTR_APPICONREFERENCE,
        ASSOCSTR_MAX
    }
}

From MSDN: https://msdn.microsoft.com/en-us/library/65kczb9y(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2

 startInfo = new ProcessStartInfo(fileName);
    if (File.Exists(fileName))
    {
        i = 0;
        foreach (String verb in startInfo.Verbs)
        {
            // Display the possible verbs.
            Console.WriteLine("  {0}. {1}", i.ToString(), verb);
            i++;
        }
    }