有时候使用不同的程序编写不同的模块,相互间少不了要传递个参数啊什么的。本程序示例简单说明如何向C#应用程序传递参数。
Program.cs中的代码如下:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace 参数调用Test
{
public class ARG
{
static string abc;
public void Arr(string[] a)
{
if (a.Length == 0)
{
//MessageBox.Show("nothing");测试时使用
abc = "nothing";//此处可以自行设置
}
else
{
for (int i = 0; i < a.Length; i++)
{
abc = abc+a[i];
}
//MessageBox.Show(abc.Length.ToString() );测试时使用
}
}
public string getContent()
{
string k = "";
k = abc;
return k;
}
}
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
ARG a = new ARG();
a.Arr(args);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Form1.cs的代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace 参数调用Test
{
public partial class Form1 : Form
{
ARG a = new ARG();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
ARG a = new ARG();
string bb = a.getContent();
textBox1.Text = bb;
}
}
}
Form1.Designer.cs的代码如下:
namespace 参数调用Test
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(364, 24);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(73, 33);
this.button1.TabIndex = 0;
this.button1.Text = "END";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(12, 12);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox1.Size = new System.Drawing.Size(317, 67);
this.textBox1.TabIndex = 1;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(491, 91);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
public System.Windows.Forms.TextBox textBox1;
}
}
程序简单界面如下:
通过命令行运行测试:
![](http://hiphotos.baidu.com/is8888/pic/item/71d07f828864328cf703a62e.jpg)