zoukankan      html  css  js  c++  java
  • 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/“);

  • 相关阅读:
    数组的学习——有序数组中插入及删除数值的问题分析
    关于控制台输入年月,显示该月日历的问题分析
    关于控制台的输入,分支结构及循环的用法的一些总结
    坚持学习,坚持写博客,记录学习的点点滴滴,先从学习JAVA开始!
    Immutable Object模式
    Windows GIT SSH 免密教程
    腾讯云服务器购买
    Centos7系统中安装JDK、Tomcat、Mysql
    SonarQube使用教程
    UEditor使用说明
  • 原文地址:https://www.cnblogs.com/ddhj/p/3080262.html
Copyright © 2011-2022 走看看