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();
}
}
}
C++ Copy Code
#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 );
}
}
}
The following example uses the Process class itself and a static Start method to start a process.
Visual Basic Copy Code
Imports System
Imports System.Diagnostics
Imports System.ComponentModel
Namespace MyProcessSample
_
'/ <summary>
'/ Shell for the sample.
'/ </summary>
Class MyProcess
'/ <summary>
'/ Opens the Internet Explorer application.
'/ </summary>
Public Sub OpenApplication(myFavoritesPath As String)
' Start Internet Explorer. Defaults to the home page.
Process.Start("IExplore.exe")
' Display the contents of the favorites folder in the browser.
Process.Start(myFavoritesPath)
End Sub 'OpenApplication
'/ <summary>
'/ Opens urls and .html documents using Internet Explorer.
'/ </summary>
Sub OpenWithArguments()
' url's are not considered documents. They can only be opened
' by passing them as arguments.
Process.Start("IExplore.exe", "www.northwindtraders.com")
' Start a Web page using a browser associated with .html and .asp files.
Process.Start("IExplore.exe", "C:\myPath\myFile.htm")
Process.Start("IExplore.exe", "C:\myPath\myFile.asp")
End Sub 'OpenWithArguments
'/ <summary>
'/ Uses the ProcessStartInfo class to start new processes, both in a minimized
'/ mode.
'/ </summary>
Sub OpenWithStartInfo()
Dim startInfo As New ProcessStartInfo("IExplore.exe")
startInfo.WindowStyle = ProcessWindowStyle.Minimized
Process.Start(startInfo)
startInfo.Arguments = "www.northwindtraders.com"
Process.Start(startInfo)
End Sub 'OpenWithStartInfo
Shared Sub Main()
' Get the path that stores favorite links.
Dim myFavoritesPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Favorites)
Dim myProcess As New MyProcess()
myProcess.OpenApplication(myFavoritesPath)
myProcess.OpenWithArguments()
myProcess.OpenWithStartInfo()
End Sub 'Main
End Class 'MyProcess
End Namespace 'MyProcessSample
C# Copy Code
using System;
using System.Diagnostics;
using System.ComponentModel;
namespace MyProcessSample
{
/// <summary>
/// Shell for the sample.
/// </summary>
class MyProcess
{
/// <summary>
/// Opens the Internet Explorer application.
/// </summary>
void OpenApplication(string myFavoritesPath)
{
// Start Internet Explorer. Defaults to the home page.
Process.Start("IExplore.exe");
// Display the contents of the favorites folder in the browser.
Process.Start(myFavoritesPath);
}
/// <summary>
/// Opens urls and .html documents using Internet Explorer.
/// </summary>
void OpenWithArguments()
{
// url's are not considered documents. They can only be opened
// by passing them as arguments.
Process.Start("IExplore.exe", "www.northwindtraders.com");
// Start a Web page using a browser associated with .html and .asp files.
Process.Start("IExplore.exe", "C:\\myPath\\myFile.htm");
Process.Start("IExplore.exe", "C:\\myPath\\myFile.asp");
}
/// <summary>
/// Uses the ProcessStartInfo class to start new processes, both in a minimized
/// mode.
/// </summary>
void OpenWithStartInfo()
{
ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
Process.Start(startInfo);
startInfo.Arguments = "www.northwindtraders.com";
Process.Start(startInfo);
}
static void Main()
{
// Get the path that stores favorite links.
string myFavoritesPath =
Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
MyProcess myProcess = new MyProcess();
myProcess.OpenApplication(myFavoritesPath);
myProcess.OpenWithArguments();
myProcess.OpenWithStartInfo();
}
}
}