一般程序中都会有如下代码 [STAThread] static void Main(string[] args) { ................ }你只要把参数传递进去就保存在string数组类型的参数args中了 在数组中保存多个值当然是可以的了。示例 1 本示例演示如何输出命令行参数。 // cmdline1.cs // arguments: A B C using System;public class CommandLine { public static void Main(string[] args) { // The Length property is used to obtain the length of the array. // Notice that Length is a read-only property: Console.WriteLine("Number of command line parameters = {0}", args.Length); for(int i = 0; i < args.Length; i++) { Console.WriteLine("Arg[{0}] = [{1}]", i, args[i]); } } } 输出 使用如下所示的一些参数运行程序:cmdline1 A B C。 输出将为: Number of command line parameters = 3 Arg[0] = [A] Arg[1] = [B] Arg[2] = [C]
示例 2 循环访问数组的另一种方法是使用 foreach 语句,如本示例所示。foreach 语句可用于循环访问数组或“.NET Framework”集合类。它提供了一种简单的方法来循环访问集合。// cmdline2.cs // arguments: John Paul Mary using System;public class CommandLine2 { public static void Main(string[] args) { Console.WriteLine("Number of command line parameters = {0}", args.Length); foreach(string s in args) { Console.WriteLine(s); } } } 输出 使用如下所示的一些参数运行程序:cmdline2 John Paul Mary。输出将为:Number of command line parameters = 3 John Paul Mary
[STAThread]
static void Main(string[] args)
{
................
}你只要把参数传递进去就保存在string数组类型的参数args中了
在数组中保存多个值当然是可以的了。示例 1
本示例演示如何输出命令行参数。
// cmdline1.cs
// arguments: A B C
using System;public class CommandLine
{
public static void Main(string[] args)
{
// The Length property is used to obtain the length of the array.
// Notice that Length is a read-only property:
Console.WriteLine("Number of command line parameters = {0}",
args.Length);
for(int i = 0; i < args.Length; i++)
{
Console.WriteLine("Arg[{0}] = [{1}]", i, args[i]);
}
}
}
输出
使用如下所示的一些参数运行程序:cmdline1 A B C。
输出将为:
Number of command line parameters = 3
Arg[0] = [A]
Arg[1] = [B]
Arg[2] = [C]
循环访问数组的另一种方法是使用 foreach 语句,如本示例所示。foreach 语句可用于循环访问数组或“.NET Framework”集合类。它提供了一种简单的方法来循环访问集合。// cmdline2.cs
// arguments: John Paul Mary
using System;public class CommandLine2
{
public static void Main(string[] args)
{
Console.WriteLine("Number of command line parameters = {0}",
args.Length);
foreach(string s in args)
{
Console.WriteLine(s);
}
}
}
输出
使用如下所示的一些参数运行程序:cmdline2 John Paul Mary。输出将为:Number of command line parameters = 3
John
Paul
Mary
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Reflection; /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Process instance = RunningInstance();
if (instance == null)
{
Application.Run(new Form1());
}
else
{
//MessageBox.Show("此程序已经启动!");
HandleRunningInstance(instance);
}
} [DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow); [DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd); private const int WS_SHOWNORMAL = 1; private static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName); foreach (Process process in processes)
{
if (process.Id!=current.Id)
{
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\")==current.MainModule.FileName)
{
return process;
}
}
} return null;
} private static void HandleRunningInstance(Process instance)
{
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL); SetForegroundWindow(instance.MainWindowHandle);
}
char *A_szClassName[MyMaxParentWinCount] = const int MyMaxParentWinCount = 5;
char *A_szClassName[MyMaxParentWinCount] =
{
"CabinetWClass",
"SHELLDLL_DefView",
"Internet Explorer_Server",
"ATL Shell Embedding",
"SysListView32",
};
char *A_szWinName[MyMaxParentWinCount] =
{
"本地磁盘(F:)",
"",
"",
"",
"",
};
HWND hLastWin = FindWindow(A_szClassName[0], A_szWinName[0]);
for(int i=1; i<MyMaxParentWinCount; i++)
{
hLastWin = FindWindowEx(hLastWin, NULL,
A_szClassName[i], A_szWinName[i]);
}
//找到这个的父窗体再通过句柄找到相关被选中文件。(以上是C++代码)
HWND WindowFromPoint( POINT Point // structure with point
);返回当前光标位置的控件句柄
http://purec.binghua.com/Article/Class12/Class14/200404/110.html