アプリケーションを起動する

C# から他のアプリケーションを起動します。

前田稔(Maeda Minoru)の超初心者のプログラム入門

プログラムの説明

  1. C# からC言語で作成した他のアプリケーションを起動してみましょう。
    Process を使用するときは Diagnostics を定義します。
    using System.Diagnostics;   // for Process
    
  2. Process.Start() でC言語で作成した CommandLine.exe を呼び出します。
    "123 abc DEFG" は CommandLine.exe に渡すパラメータです。
    process_1.WaitForExit(); で CommandLine.exe が終了するのを待ちます。
    起動したアプリケーションが終了すれば C# に戻ります。
    /*********************************************/
    /*★ Process でプログラムを起動    前田 稔 ★*/
    /*********************************************/
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Diagnostics;   // for Process
    
    public class MyForm : Form
    {
        public MyForm()
        {   Text = "Window のキャプションです";
            BackColor = SystemColors.ControlLight;  
        }
    }
    
    class form01
    {
        public static void Main()
        {   System.Diagnostics.Process process_1 =
                Process.Start("C:\\Data\\CommandLine.exe","123 abc DEFG");
            process_1.WaitForExit();
            Console.WriteLine("プログラムの終了\n");
    
            MyForm mf = new MyForm();
            Application.Run(mf);
        }
    }
    
  3. C言語で作成した CommandLine.exe のソースコードです。
    /*******************************/
    /*★ Command Line    前田 稔 ★*/
    /*******************************/
    #include <stdio.h>
    
    int main(int argc, char* argv[])
    {
        for(int i=0; i<argc; i++)
            printf("%d %s\n", i,argv[i]);
        return(0);
    }
    

関連付けられたアプリケーションから開く

  1. ファイルを関連付けられたアプリケーションから開く場合も全く同じです。
    事前に "C:\\Data\\Test\\data.txt" を格納しておいて下さい。
    今回は "data.txt" を起動したら MyForm() を呼び出してウインドウの枠を表示します。
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Diagnostics;   // for Process
    
    public class MyForm : Form
    {
        public MyForm()
        {   Text = "Window のキャプションです";
            BackColor = SystemColors.ControlLight;  
        }
    }
    
    class form01
    {
        public static void Main()
        {   System.Diagnostics.Process.Start("C:\\Data\\Test\\data.txt");
    
            MyForm mf = new MyForm();
            Application.Run(mf);
        }
    }
    

超初心者のプログラム入門(C# Frame Work)