C#中关于进程的一些常用操作

    public static void StartProgram(string directory,string fileName)
    {
        Process mainprocess = new Process();
        mainprocess.StartInfo.WorkingDirectory = directory;
        mainprocess.StartInfo.FileName = fileName;//其实可以不指定上面的目录,这里写成directory + filename亦可
        mainprocess.StartInfo.Arguments = "ok";//这里可以给其他进程(exe文件)传递参数
        mainprocess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
        mainprocess.Start();
    }

    //另一进程
    static Class Program
    {
        static void Main(string[] arguments) {    
                string arg = arguments[0];//arg="ok";
        }

    }  
启动进程
            //杀死进程,这里的processName不能带.exe,谨记!!!
        public static void KillProcess(string processName)
        {
            Process myproc = new Process();
            try
            {
                foreach (Process proc in Process.GetProcessesByName(processName))
                {
                    if (!proc.CloseMainWindow())
                    {
                        proc.Kill();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
杀死进程
         private void PrintDoc()
        {
            //定义一个进程实例
            System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
            try
            {
                //设置进程的参数
                string myDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                myProcess.StartInfo.FileName = myDocumentsPath + "\TxtForTest.txt";
                myProcess.StartInfo.Verb = "Print";
                //显示txt文件的所有谓词
                foreach (string v in myProcess.StartInfo.Verbs)
                MessageBox.Show(v);
                myProcess.StartInfo.CreateNoWindow = true;
                //启动进程
                myProcess.Start();
            }
            catch (Win32Exception e)
            {
                if (e.NativeErrorCode == ERROR_FILE_NOT_FOUND)
                {
                MessageBox.Show(e.Message + " Check the path." + myProcess.StartInfo.FileName);
                }
                else if (e.NativeErrorCode == ERROR_ACCESS_DENIED)
                {
                MessageBox.Show(e.Message + " You do not have permission to print this file.");
                }
            }
        }
打印文档
//启动浏览器:
Process.Start("IExplore.exe");
//打开浏览器,并跳转到指定的网址:
Process.Start("IExplore.exe", "http://www.cnblogs.com/ddhj/");

如果Start的是文件夹路径,而不是文件路径,将会打开此文件夹:
string myFavoritesPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Favorites);//获取“收藏夹”文件路径
//启动进程
System.Diagnostics.Process.Start(myFavoritesPath);

打开文件夹:

System.Diagnostics.Process.Start(FilePath);

打开文件夹中某个文件:

System.Diagnostics.Process.Start(FilePath+"/"+FileName);

打开文件夹并选中单个文件:

System.Diagnostics.Process.Start("Explorer", "/select,"+ FilePath+"\"+FileName);

System.Diagnostics.Process.Start("Explorer.exe", "/select,"+ FilePath+"\"+FileName);

用IE打开文件:

System.Diagnostics.Process.Start("Explorer",FilePath+"\"+FileName);

System.Diagnostics.Process.Start("Explorer.exe",FilePath+"\"+FileName);

注:(explorer,explorer.exe,select,不区分大小写,"/selecet,"其中"/,"都不能少,FilePath为文件路径不包含文件名)

 打开文件夹并选中网页

System.Diagnostics.Process.Start("Explorer.exe", ”http://www.sunwale.com/“);

 

posted on 2013-05-15 17:35  fish_1949  阅读(389)  评论(0编辑  收藏  举报

导航