ProcessStartInfo.FileName 属性
获取或设置要启动的应用程序或文档。
**命名空间:**System.Diagnostics
**程序集:**System(在 system.dll 中)
语法
声明
Public Property FileName As String
用法
Dim instance As ProcessStartInfo
Dim value As String
value = instance.FileName
instance.FileName = value
public string FileName { get; set; }
public:
property String^ FileName {
String^ get ();
void set (String^ value);
}
/** @property */
public String get_FileName ()
/** @property */
public void set_FileName (String value)
public function get FileName () : String
public function set FileName (value : String)
属性值
要启动的应用程序的名称或某文件类型的文档的名称,该文件类型与应用程序关联并且拥有可用的默认打开操作。默认值为空字符串 ("")。
备注
启动该进程前至少必须设置 FileName 属性。文件名是任何应用程序或文档。此处,将文档定义为具有与其关联的打开或默认操作的任何文件类型。您可以通过操作系统的“文件夹选项”对话框来查看计算机的已注册文件类型及其关联应用程序。单击“高级”按钮可打开一个对话框,该对话框显示是否存在与特定的注册文件类型相关联的打开操作。
可用的文件类型集部分取决于 UseShellExecute 属性的值。如果 UseShellExecute 为 true,则您能够使用 Process 组件启动任何文档并对文件执行操作(如打印)。当 UseShellExecute 为 false 时,使用 Process 组件仅能启动可执行文件。
示例
Imports System
Imports System.Diagnostics
Imports System.ComponentModel
Namespace MyProcessSample
_
'/ <summary>
'/ Shell for the sample.
'/ </summary>
Class MyProcess
' These are the Win32 error code for file not found or access denied.
Private ERROR_FILE_NOT_FOUND As Integer = 2
Private ERROR_ACCESS_DENIED As Integer = 5
'/ <summary>
'/ Prints a file with a .doc extension.
'/ </summary>
Sub PrintDoc()
Dim myProcess As New Process()
Try
' Get the path that stores user documents.
Dim myDocumentsPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
myProcess.StartInfo.FileName = myDocumentsPath + "\MyFile.doc"
myProcess.StartInfo.Verb = "Print"
myProcess.StartInfo.CreateNoWindow = True
myProcess.Start()
Catch e As Win32Exception
If e.NativeErrorCode = ERROR_FILE_NOT_FOUND Then
Console.WriteLine((e.Message + ". Check the path."))
Else
If e.NativeErrorCode = ERROR_ACCESS_DENIED Then
' Note that if your word processor might generate exceptions
' such as this, which are handled first.
Console.WriteLine((e.Message + ". You do not have permission to print this file."))
End If
End If
End Try
End Sub 'PrintDoc
Public Shared Sub Main()
Dim myProcess As New MyProcess()
myProcess.PrintDoc()
End Sub 'Main
End Class 'MyProcess
End Namespace 'MyProcessSample
using System;
using System.Diagnostics;
using System.ComponentModel;
namespace MyProcessSample
{
/// <summary>
/// Shell for the sample.
/// </summary>
class MyProcess
{
// These are the Win32 error code for file not found or access denied.
const int ERROR_FILE_NOT_FOUND =2;
const int ERROR_ACCESS_DENIED = 5;
/// <summary>
/// Prints a file with a .doc extension.
/// </summary>
void PrintDoc()
{
Process myProcess = new Process();
try
{
// Get the path that stores user documents.
string myDocumentsPath =
Environment.GetFolderPath(Environment.SpecialFolder.Personal);
myProcess.StartInfo.FileName = myDocumentsPath + "\\MyFile.doc";
myProcess.StartInfo.Verb = "Print";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
}
catch (Win32Exception e)
{
if(e.NativeErrorCode == ERROR_FILE_NOT_FOUND)
{
Console.WriteLine(e.Message + ". Check the path.");
}
else if (e.NativeErrorCode == ERROR_ACCESS_DENIED)
{
// Note that if your word processor might generate exceptions
// such as this, which are handled first.
Console.WriteLine(e.Message +
". You do not have permission to print this file.");
}
}
}
public static void Main()
{
MyProcess myProcess = new MyProcess();
myProcess.PrintDoc();
}
}
}
#using <System.dll>
using namespace System;
using namespace System::Diagnostics;
using namespace System::ComponentModel;
// These are the Win32 error code for file not found or access denied.
#define ERROR_FILE_NOT_FOUND 2
#define ERROR_ACCESS_DENIED 5
int main()
{
Process^ myProcess = gcnew Process;
try
{
// Get the path that stores user documents.
String^ myDocumentsPath = Environment::GetFolderPath( Environment::SpecialFolder::Personal );
myProcess->StartInfo->FileName = String::Concat( myDocumentsPath, "\\MyFile.doc" );
myProcess->StartInfo->Verb = "Print";
myProcess->StartInfo->CreateNoWindow = true;
myProcess->Start();
}
catch ( Win32Exception^ e )
{
if ( e->NativeErrorCode == ERROR_FILE_NOT_FOUND )
{
Console::WriteLine( "{0}. Check the path.", e->Message );
}
else
if ( e->NativeErrorCode == ERROR_ACCESS_DENIED )
{
// Note that if your word processor might generate exceptions
// such as this, which are handled first.
Console::WriteLine( "{0}. You do not have permission to print this file.", e->Message );
}
}
}
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0
请参见
参考
ProcessStartInfo 类
ProcessStartInfo 成员
System.Diagnostics 命名空间