c#控制台应用程序,使用变量命名进程
本文关键字:变量 进程 控制台 应用程序 | 更新日期: 2023-09-27 18:08:03
我有一个c#控制台应用程序,它为15台数码相机创建了一个进程,可以根据命令拍摄和下载照片。目前,它是为16个相机硬编码的,我想让它向用户询问相机的数量,然后创建这个数量的进程/连接。
下面是连接3个摄像头的代码示例:
Process takePic2;
takePic2 = new Process();
takePic2.StartInfo.UseShellExecute = false;
//takePic2.StartInfo.RedirectStandardOutput = true;
takePic2.StartInfo.RedirectStandardInput = true;
takePic2.StartInfo.FileName = chdkLocation;
string config2 = @"-i -c""-d=''.'libusb0%-0002%-%-0x04a9%-0x3248""";
takePic2.StartInfo.Arguments = string.Format(config2);
takePic2.Start();
StreamWriter sw2 = takePic2.StandardInput;
//sw2.WriteLine("list");
sw2.WriteLine("rec");
//sw2.WriteLine("shoot");
Process takePic3;
takePic3 = new Process();
takePic3.StartInfo.UseShellExecute = false;
//takePic3.StartInfo.RedirectStandardOutput = true;
takePic3.StartInfo.RedirectStandardInput = true;
takePic3.StartInfo.FileName = chdkLocation;
string config3 = @"-i -c""-d=''.'libusb0%-0003%-%-0x04a9%-0x3248""";
takePic3.StartInfo.Arguments = string.Format(config3);
takePic3.Start();
StreamWriter sw3 = takePic3.StandardInput;
//sw3.WriteLine("list");
sw3.WriteLine("rec");
//sw3.WriteLine("shoot");
Process takePic4;
takePic4 = new Process();
takePic4.StartInfo.UseShellExecute = false;
//takePic4.StartInfo.RedirectStandardOutput = true;
takePic4.StartInfo.RedirectStandardInput = true;
takePic4.StartInfo.FileName = chdkLocation;
string config4 = @"-i -c""-d=''.'libusb0%-0004%-%-0x04a9%-0x3248""";
takePic4.StartInfo.Arguments = string.Format(config4);
takePic4.Start();
StreamWriter sw4 = takePic4.StandardInput;
//sw4.WriteLine("list");
sw4.WriteLine("rec");
//sw4.WriteLine("shoot");
,这里是获取图片并下载它们的代码:
while (true)
{
Console.WriteLine("type shoot to take a picture:"); // Prompt
string commandInput = Console.ReadLine(); // Get string from user
if (commandInput == "s")
{
sw1.WriteLine("shoot");
sw2.WriteLine("shoot");
sw3.WriteLine("shoot");
sw4.WriteLine("shoot");
sw5.WriteLine("shoot");
sw6.WriteLine("shoot");
sw7.WriteLine("shoot");
sw8.WriteLine("shoot");
sw9.WriteLine("shoot");
sw10.WriteLine("shoot");
sw11.WriteLine("shoot");
sw12.WriteLine("shoot");
sw13.WriteLine("shoot");
sw14.WriteLine("shoot");
sw15.WriteLine("shoot");
sw16.WriteLine("shoot");
}
if (commandInput == "dl")
{
sw1.WriteLine("imdl -ddir=/HDimages/camera1/ -rm");
sw2.WriteLine("imdl -ddir=/HDimages/camera2/ -rm");
sw3.WriteLine("imdl -ddir=/HDimages/camera3/ -rm");
sw4.WriteLine("imdl -ddir=/HDimages/camera4/ -rm");
sw5.WriteLine("imdl -ddir=/HDimages/camera5/ -rm");
sw6.WriteLine("imdl -ddir=/HDimages/camera6/ -rm");
sw7.WriteLine("imdl -ddir=/HDimages/camera7/ -rm");
sw8.WriteLine("imdl -ddir=/HDimages/camera8/ -rm");
sw9.WriteLine("imdl -ddir=/HDimages/camera9/ -rm");
sw10.WriteLine("imdl -ddir=/HDimages/camera10/ -rm");
sw11.WriteLine("imdl -ddir=/HDimages/camera11/ -rm");
sw12.WriteLine("imdl -ddir=/HDimages/camera12/ -rm");
sw13.WriteLine("imdl -ddir=/HDimages/camera13/ -rm");
sw14.WriteLine("imdl -ddir=/HDimages/camera14/ -rm");
sw15.WriteLine("imdl -ddir=/HDimages/camera15/ -rm");
sw16.WriteLine("imdl -ddir=/HDimages/camera16/ -rm");
}
}
我已经让它向用户询问要连接的相机的数量,并为该数字创建了一个for循环。我的问题是我如何设置进程名递增与循环I变量,所以它将是"进程takepic+ camerannumber"
Console.WriteLine("Enter number of cameras:"); // Prompt
string inputCameraQuantity = Console.ReadLine(); // Get string from user
int cameraQuantity = Convert.ToInt32(inputCameraQuantity);
string chdkLocation = @"C:'scancode'chdk'chdkptp.exe";
for (int i = 1; i <= cameraQuantity; i++)
{
string cameranumber = Convert.ToString(i);
Process takepic;
//Process takepic+cameranumber;
takepic = new Process();
takepic.StartInfo.UseShellExecute = false;
takepic.StartInfo.RedirectStandardInput = true;
takepic.StartInfo.FileName = chdkLocation;
if (cameraQuantity < 10)
{
cameranumber = "0" + Convert.ToString(i);
}
else { cameranumber = Convert.ToString(i); }
string configA = @"-i -c""-d=''.'libusb0%-000";
string configB = @"%-%-0x04a9%-0x3248""";
string configC = configA + cameranumber + configB;
}
你不能。相反,你可以把它放到List中。
var Cameras = new List<Process>()
for (int i = 1; i <= cameraQuantity; i++)
{
Process takepic;
//Process takepic+cameranumber;
takepic = new Process();
Cameras.Add(takepic);
那么你可以通过
得到它们Cameras[0]. //whatever
或
如果你想给它们命名,把它们放在字典里
var Cameras = new Dictionary<string, Process>()
for (int i = 1; i <= cameraQuantity; i++)
{
Process takepic;
//Process takepic+cameranumber;
takepic = new Process();
Cameras.Add("takepic"+cameranumber, takepic);
然后得到它们
Cameras["takepic1"]. // whatever