ProcessStartInfo.Verbs プロパティ

定義

FileName プロパティで指定したファイルの種類に関連付けられている動詞のセットを取得します。

public:
 property cli::array <System::String ^> ^ Verbs { cli::array <System::String ^> ^ get(); };
public string[] Verbs { get; }
[System.ComponentModel.Browsable(false)]
public string[] Verbs { get; }
member this.Verbs : string[]
[<System.ComponentModel.Browsable(false)>]
member this.Verbs : string[]
Public ReadOnly Property Verbs As String()

プロパティ値

String[]

FileName プロパティで示されたファイルにシステムが適用できるアクション。

属性

次のコード例では、選択したファイル名の定義された動詞を表示します。 ユーザーが定義された動詞のいずれかを選択した場合、この例では、選択した動詞と入力ファイル名を使用して新しいプロセスを開始します。

using System;
using System.ComponentModel;
using System.IO;
using System.Diagnostics;
using System.Windows.Forms;

class ProcessInformation
{
    [STAThread]
    static void Main()
    {
        OpenFileDialog openFileDialog1 = new OpenFileDialog();

        openFileDialog1.InitialDirectory = "c:\\";
        openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
        openFileDialog1.FilterIndex = 2;
        openFileDialog1.RestoreDirectory = true;
        openFileDialog1.CheckFileExists = true;

        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            var fileName = openFileDialog1.FileName;

            int i = 0;
            var startInfo = new ProcessStartInfo(fileName);

            // Display the possible verbs.
            foreach (var verb in startInfo.Verbs)
            {
                Console.WriteLine($"  {i++}. {verb}");
            }

            Console.Write("Select the index of the verb: ");
            var indexInput = Console.ReadLine();
            int index;
            if (Int32.TryParse(indexInput, out index))
            {
                if (index < 0 || index >= i)
                {
                    Console.WriteLine("Invalid index value.");
                    return;
                }

                var verbToUse = startInfo.Verbs[index];

                startInfo.Verb = verbToUse;
                if (verbToUse.ToLower().IndexOf("printto") >= 0)
                {
                    // printto implies a specific printer.  Ask for the network address.
                    // The address must be in the form \\server\printer.
                    // The printer address is passed as the Arguments property.
                    Console.Write("Enter the network address of the target printer: ");
                    var arguments = Console.ReadLine();
                    startInfo.Arguments = arguments;
                }

                try
                {
                    using (var newProcess = new Process())
                    {
                        newProcess.StartInfo = startInfo;
                        newProcess.Start();

                        Console.WriteLine($"{newProcess.ProcessName} for file {fileName} " +
                                          $"started successfully with verb '{startInfo.Verb}'!");
                    }
                }
                catch (Win32Exception e)
                {
                    Console.WriteLine("  Win32Exception caught!");
                    Console.WriteLine($"  Win32 error = {e.Message}");
                }
                catch (InvalidOperationException)
                {
                    // Catch this exception if the process exits quickly,
                    // and the properties are not accessible.
                    Console.WriteLine($"Unable to start '{fileName}' with verb {verbToUse}");
                }
            }
        }
        else
        {
            {
                Console.WriteLine("You did not enter a number.");
            }
        }
    }
}
Imports System.ComponentModel
Imports System.IO
Imports System.Diagnostics
Imports System.Windows.Forms

Module ProcessInformation
    Public Shared Sub Main()
        Dim openFileDialog1 As New OpenFileDialog()

        openFileDialog1.InitialDirectory = "c:\"
        openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        openFileDialog1.FilterIndex = 2
        openFileDialog1.RestoreDirectory = True
        openFileDialog1.CheckFileExists = True

        If openFileDialog1.ShowDialog() = DialogResult.OK Then
            Dim fileName = openFileDialog1.FileName

            Dim i = 0
            Dim startInfo = New ProcessStartInfo(fileName)

            Dim verb As String
            For Each verb In startInfo.Verbs
                ' Display the possible verbs.
                Console.WriteLine($"  {i}. {verb}")
                i += 1
            Next

            Console.Write("Select the index of the verb: ")
            Dim indexInput = Console.ReadLine()
            Dim index As Integer
            If Int32.TryParse(indexInput, index) Then
                If index < 0 OrElse index >= i Then
                    Console.WriteLine("Invalid index value.")
                    Return
                End If

                Dim verbToUse = startInfo.Verbs(Convert.ToInt32(index))

                startInfo.Verb = verbToUse
                If verbToUse.ToLower().IndexOf("printto") >= 0 Then
                    ' printto implies a specific printer.  Ask for the network address.
                    ' The address must be in the form \\server\printer.
                    Console.Write("Enter the network address of the target printer: ")
                    Dim arguments = Console.ReadLine()
                    startInfo.Arguments = arguments
                End If

                Try
                    Using newProcess As New Process
                        newProcess.StartInfo = startInfo
                        newProcess.Start()

                        Console.WriteLine($"{newProcess.ProcessName} for file {fileName} " +
                                          $"started successfully with verb '{startInfo.Verb}'!")
                    End Using
                Catch e As Win32Exception
                    Console.WriteLine("  Win32Exception caught!")
                    Console.WriteLine($"  Win32 error = {e.Message}")
                Catch e As InvalidOperationException
                    Console.WriteLine($"Unable to start '{fileName}' with verb {verbToUse}")
                End Try
            Else
                Console.WriteLine("You did not enter a number.")
            End If
        End If
    End Sub
End Module

注釈

この Verbs プロパティを使用すると、プロパティで指定されたファイルで使用できる動詞を FileName 決定できます。 このプロパティは Verb 、セット内の任意の動詞の値に設定できます。 動詞の例として、"Edit"、"Open"、"OpenAsReadOnly"、"Print"、"Printto" があります。

動詞の結果の一覧に使用可能なすべての値が含まれているわけではないことに注意してください。たとえば、"New" は仕様によってリストに追加されず、システムのセットアップによっては他の可能な動詞が常に検出されるとは限りません。

プロパティを Verbs 使用する場合は、プロパティの値を設定するときにファイル名拡張子を FileName 含める必要があります。 ファイル名拡張子は、使用可能な動詞のセットを決定します。

適用対象

こちらもご覧ください