首页 新闻 会员 周边

C# 关于使用cmd命令的问题。

0
[待解决问题]

    最近的程序需要在程序中使用一些配置命令,于是就用了cmd命令(批处理脚本) 

(XP 系统)
  命令功能:获取本机的计算机名或IP地址 然后映射共享打印机的端口,由于用户那边的机器 即使了 用了 persistent:yes命令,再次启动命令之前的命令就会失效,不知为何,据说那边没有安装还原精灵之类的软件。

  现在的问题是:用户那边将程序做了开机启动,程序中已经进行了脚本处理,而且也会生成xxx.bat文件,但是每次用户那边长时间关机后开机,第一次运行程序 cmd 命令都不会成功(命令是对的)第二次运行才成功

  bat文件不会自动运行,只是生成该文件

  请问大大们这是什么原因。[/color]


程序代码(cmd部分):
         using (Process p = new Process())
                {
                    p.StartInfo.RedirectStandardError = true;
                    p.StartInfo.RedirectStandardInput = true;
                    p.StartInfo.RedirectStandardOutput = true;
                    p.StartInfo.UseShellExecute = false;
                    p.StartInfo.CreateNoWindow = true;
                    p.StartInfo.FileName = "C:\\WINDOWS\\system32\\cmd.exe"; 
                    p.Start();
                    p.StandardInput.WriteLine("net use " + this.portName + ": \\\\" + ipAddress + "\\" + this.sharePrinter + " /persistent:yes");
                    p.StandardInput.WriteLine("net use " + this.portName + ": /delete");
                    p.StandardInput.WriteLine("net use " + this.portName + ": \\\\"+ ipAddress + "\\" + this.sharePrinter + " /persistent:yes");
                    p.StandardInput.WriteLine("exit");
                    p.WaitForExit();

                    string s = p.StandardError.ReadToEnd();
                    if (!string.IsNullOrEmpty(s))
                    {
                        Logger.appendLog(s);
                    }
                }

ipAddress 用计算机名称也是同样如此。


在自己的计算机上怎么也不能复现出来,即使是当天关机第二天开机...
谢谢大家了!!!

没用过剃须刀的主页 没用过剃须刀 | 菜鸟二级 | 园豆:208
提问于:2014-09-02 13:07
< >
分享
所有回答(2)
0

那就自动运行两次好了。

Firen | 园豆:5385 (大侠五级) | 2014-09-03 12:12

上面的已经是两次的了。。。。

支持(0) 反对(0) 没用过剃须刀 | 园豆:208 (菜鸟二级) | 2014-09-03 12:36
0

在批处理最上层加延时调试

LarryIsTaken | 园豆:208 (菜鸟二级) | 2015-06-08 17:13
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册