ProcessStartInfo.Verbs 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得與屬性指定 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 為集合中任意動詞的值。 動詞的例子有「編輯」、「開放」、「OpenAsReadOnly」、「列印」和「列印」。
請注意,最終產生的動詞列表可能不包含所有可能的值,例如「New」設計上不會被加入清單,且根據系統設定,其他可能的動詞也不一定能被偵測到。
使用 Verbs 屬性時,設定屬性 FileName 值時必須包含檔名副檔名。 檔名副檔名決定了可能的動詞集合。