在1个系统窗口中执行多个dos命令 [英] Execute More than one dos command in 1 System window

查看:116
本文介绍了在1个系统窗口中执行多个dos命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我写了一些代码.
我很好地实现了"net use命令"

=======

So I wrote Some Code..
I implemented the "net use command nicely"

=======

System.Diagnostics.Process login;
login = new System.Diagnostics.Process();

string cleardrives;
cleardrives = "/C net use * /delete /y";





string mapDriveP;
mapDriveP = "/C net use t: \\\\SERVER\\multimedia /user:"+ textBox1.Text + " " + textBox2.Text;
string mapDriveS;
mapDriveS = "/C net use s: \\\\SERVER\\temp /user:" + textBox1.Text + " " + textBox2.Text;
string mapDriveU;
mapDriveU = "/C net use u: \\\\SERVER\\USER /user:" + textBox1.Text + " " + textBox2.Text;





System.Diagnostics.Process.Start("CMD.exe", cleardrives);
System.Threading.Thread.Sleep(4000);
System.Diagnostics.Process.Start("CMD.exe", mapDriveP);
System.Threading.Thread.Sleep(2000);
System.Diagnostics.Process.Start("CMD.exe", mapDriveS);
System.Threading.Thread.Sleep(2000);
System.Diagnostics.Process.Start("CMD.exe", mapDriveU);
login.Close();



=================

这行得通..问题是,第一个驱动器有时需要一些时间来映射
我只想在一个系统窗口中执行所有map命令.

我考虑过要在代码中实现.bat文件,但我陷入了texbox字段(网络使用的用户名和密码
)的插入问题 命令)

任何帮助将不胜感激.

再次出现问题:如何在一个系统窗口中启动MAPDRIVEP,MAPDRIVES和MAPDRIVEU,现在出现3个systemwindow弹出窗口.
[edit]删除了虚假代码块-OriginalGriff [/edit]



=================

This works.. the problem is sometimes it takes some time for the first drive to map
and I just want to execute all the map commands in one system window.

I thought about implementing a .bat file in the code but I got stuck on the insertion of the texbox fields (which are the username and password for the net use
command)

Any help would be gratly appreciated.

Again the question HOW DO I START THE MAPDRIVEP, MAPDRIVES and MAPDRIVEU in one system window now I get 3 systemwindow popups..

[edit]Spurious code block removed - OriginalGriff[/edit]

推荐答案

仅需注意:没有"DOS命令"之类的东西.如果您正在谈论"CMD.EXE",这只是使用控制台并运行命令解释器的常规Windows受更多保护的应用程序.

您无需根据情况运行它.您需要运行"NET.EXE"本身:

Just a note: there is not such thing as "DOS Command". If you''re talking about "CMD.EXE" — this is just a regular Windows protected-more application using console and running command interpreter.

You don''t need to run it in your case. You need to run "NET.EXE" itself:

string cmdLine = string.Format("{0} {1} {2}", mapDriveP + mapDriveU + mapDriveS);
System.Diagnostics.Process.Start("NET.EXE", cmdLine);



如果在仅运行一个"NET.EXE"会话时遇到问题,可以考虑一个一个地运行它:



If you have a problem running only one "NET.EXE" session you can consider running it one-by-one:

string[] parameters = new string[] {
@"use u: \\server\multimedia /user:""indiana jones""",
@"use p: \\server\temp /user:""indiana jones""",
@"use s: \\server\user /user:""indiana jones""", };

//warning: I suspect must be /user:"Indiana Jones" (in quotation)

foreach(string cmdLineParameter in parameters) {
   System.Diagnostics.Process process = 
      System.Diagnostics.Process.Start("NET.EXE", cmdLineParameter);
   process.WaitForExit();
}



如果"NET.EXE"过程的一个实例不依赖于另一个实例的结果,则可以省略WaitForExit,但是如果进一步的代码依赖于结果,则无论如何都需要等待.

另一种选择是使用批处理文件.



If one instance of the "NET.EXE" process does not depend on the result of another one, you can omit WaitForExit, but if you further code depends on the results, you need to wait anyway.

Another option is using batch file.

System.Diagnostics.Process.Start("myBatchFile.bat");



请勿使用Sleep!不要使用"CMD.EXE" !!!

—SA



Do not use Sleep! Do not use "CMD.EXE"!!!

—SA


感谢dforbes的回复
但我一直在考虑像...
这样的解决方案
Thanks for the reply dforbes
but I was thinking more along a solution like ...

System.Diagnostics.Process.Start("CMD.exe", mapDriveP + mapDriveU + mapDriveS);



像这样..只有一个系统窗口打开,并且所有驱动器映射字符串都是
已执行..原始批处理文件具有3个净使用行..

========
净使用u:\\ server \ multimedia/user:indiana jones
净使用p:\\ server \ temp/user:indiana jones
净使用s:\\ server \ user/user:indiana jones

=======

问题是如果我给每个用户自己的文件,他们可能会编辑"
蝙蝠文件,并查看彼此的密码.
我什至反对为每个用户制作一个exe文件:-)



Something like that.. that only 1 system window opens and all the drive map strings are
executed.. the original batch file has 3 net use lines ..

========
net use u: \\server\multimedia /user:indiana jones
net use p: \\server\temp /user:indiana jones
net use s: \\server\user /user:indiana jones

=======

the problem with this is if I give every user his own file they might "edit"
the bat files and see eachothers password.
I even thaugt of making an exe for each user :-)


只是好奇(你知道那只猫怎么了)...

为什么不使用NET USE [驱动器字母] [UNC路径] .....?
您可以将%1,%2,%3 ...用于变量.
拖出旧的DOS 5.0手册.它有一个很好的部分介绍了它是如何工作的.对于较新版本的Windows,您可以将扩展名更改为".cmd"(无关紧要).您可以将变量直接传递到命令窗口.

希望这可以帮助.不要重新发明轮子. DOS很旧,但是非常稳定,您可以得到预期的结果.
Just curious (you know what happened to the cat)...

Why not use NET USE [DRIVE LETTER] [UNC PATH].....?
You can use %1, %2, %3... for variables.
Drag out an old DOS 5.0 manual. It has a great section on how it works. You can change the extension to ''.cmd'' for newer versions of Windows (not that it matters). You can pass the variables directly to the command window.

Hope this helps. Don''t re-invent the wheel. DOS is old, but very stable, and you get expected results.


这篇关于在1个系统窗口中执行多个dos命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆