现有2个程序 A和B,程序B必须通过程序A去调用才能运行,而程序B不能直接双击去单独运行~~~~~这样子的话,程序B里面应该怎样写代码,才能做到这个效果啊????各位高手!!!

解决方案 »

  1.   

    如果A,B两个程序都是你自己写的话可以用参数啊。
    B程序Program.cs写成这样:
    static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                if (args.Length <= 0)
                {
                    Application.Exit();
                }
                if (args.Length == 1)
                {
                    if (args[0] == "B")
                    {
                        Application.Run(new Form1());
                    }
                    else
                    {
                        Application.Exit();
                    }
                }
            }
        }A的form中放一个输入框输入参数(当然你可以用配置文件),放一个按钮启动B
    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        public bool StartProcess(string filename, string[] args)
            {
                try
                {
                    string s = "";
                    foreach (string arg in args)
                    {
                        s = s + arg + " ";
                    }
                    s = s.Trim();
                    Process myprocess = new Process();
                    ProcessStartInfo startInfo = new ProcessStartInfo(filename, s);
                    myprocess.StartInfo = startInfo;
                    myprocess.StartInfo.UseShellExecute = false;
                    myprocess.Start();
                    return true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("启动应用程序时出错!原因:" + ex.Message);
                }
                return false;
            } 
            private void button1_Click(object sender, EventArgs e)
            {
                string[] arg = new string[1];
                arg[0] = textBox1.Text.Trim();
                StartProcess(@"F:\test\B\B\bin\Debug\B.exe", arg);
            }
        }