2010-01-19 6 views

Répondre

1

utilisation des capacités de classe à system.diagnostics.process fais ceci.

using System; 
using System.Diagnostics; 
using System.ComponentModel; 

namespace MyProcessSample 
{ 
/// <summary> 
/// Shell for the sample. 
/// </summary> 
public 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> 
    public 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."); 
      } 
     } 
    } 
+0

mais comment obtenir le chemin d'exe fichier – Swati

+0

S'il vous plaît clarifier plus? Que voulez-vous dire par obtenir le chemin? Trouver le chemin absolu lorsqu'on lui donne juste le nom du fichier? –

1

Il semble que vous souhaitiez lancer un processus distinct.

Process myProcess = new Process(); 
myProcess.StartInfo.FileName = @"c:\program files\hello\abc.exe" 
myProcess.StartInfo.CreateNoWindow = true; 
myProcess.Start(); 
+0

@ "c: \ program files \ hello \ abc.exe" Je ne peux pas définir le chemin comme ceci, il devrait être relatif – Swati

+0

Vous pouvez utiliser Directory.GetCurrentDirectory() pour obtenir le répertoire courant, et construire un chemin relatif à partir de ce répertoire . –

+0

merci 2 tout, mon travail est fait !!!!!!!!!!!!!!!! – Swati

Questions connexes