玄铁剑

成功的途径:抄,创造,研究,发明...
posts - 128, comments - 42, trackbacks - 0, articles - 174

Process cmd

Posted on 2007-06-17 13:18 玄铁剑 阅读(935) 评论(6)  编辑 收藏 引用 所属分类: windows

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();

         } 
 }
}

 

Feedback

# re: Process cmd  回复  更多评论   

2012-03-08 08:44 by TriciaBanks
I think it's attractive, because it emblazon a very academic-focussed attitude. Preserving the forthrightness of the academic system seems to be a priority, although honestly enjoin paid ads for such a service seems a fragile response. When the association is known by your friends who were delighted with the results of the collaboration, about this address that for the <a href="http://www.thesisleader.com">dissertation service</a> quality.
只有注册用户登录后才能发表评论。