【问题标题】:Running multiple commands in a command line utility using cmd.exe from .Net win form使用 .Net win 形式的 cmd.exe 在命令行实用程序中运行多个命令
【发布时间】:2013-02-15 14:49:29
【问题描述】:

我想运行 tabcmd.exe utility 在 tableau server 中发布视图。执行此操作的手动步骤如下,日志文件将针对位置中的每个步骤进行更新

“C:\Users[用户名]\AppData\Roaming\Tableau\tabcmd.log”

在同一会话中,我必须逐个手动执行此步骤

  1. 运行 cmd.exe
  2. 使用命令登录tabcmd login -s "http:/sales-server:8000" -t Sales -u administrator -p p@ssw0rd!
  3. 使用命令创建项目名称 tabcmd createproject -n "Quarterly_Reports" -d "显示季度销售报告的工作簿。"
  4. 使用命令发布视图 tabcmd publish "analysis.twbx" -n "Sales_Analysis" --db-user "jsmith" --db-password "p@ssw0rd"
  5. 使用命令刷新tabcmd refreshextracts --workbook "My Workbook"
  6. 使用命令 tabcmd logout 注销

现在我正在尝试从我的 .Net win 表单中自动执行这些步骤,因此我尝试使用下面的代码,但它不起作用。

String path = @"C:\Program Files (x86)\Tableau\Tableau Server\7.0\bin\tabcmd.exe"
ProcessStartInfo startInfo = new ProcessStartInfo ();
startInfo.FileName = "\""+path+ "\"";
startInfo.Arguments = String.Format("login -s http://Server1:8000 --db-user "jsmith" --db-password "p@ssw0rd");
startInfo.UseShellExecute = false ;
startInfo.CreateNoWindow = false;
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardError = true;
Process p = new Process();
p.StartInfo = startInfo;
p.Start();      


using (StreamWriter sw = p.StandardInput)
        {
            if (sw.BaseStream.CanWrite)
            {
                sw.WriteLine("createproject -n \"MyProject\" -d \"MyProjectWorkbook\"");
                //sw.WriteLine("My next Command");
                //sw.WriteLine("My next Command");
            }
        }  

我能够成功登录,但我无法进一步执行后续步骤,我不知道如何进一步进行此操作,因此我期待一些帮助。 提前致谢!

【问题讨论】:

  • 重定向输出但不实际读取它是一个坏主意。当程序的输出缓冲区已满并且由于您没有阅读它而无法清空时,该程序将挂起。速度也很快,通常约为 2 KB。

标签: c# winforms cmd tableau-api


【解决方案1】:

我不确定这是否对您有用,但您可以尝试使用所有这些命令创建一个批处理文件,然后从命令提示符执行批处理文件,如下所示:-

  //Build Commands - You may have to play with the syntax

    string[] cmdBuilder = new string[5] 
      { 
       @"tabcmd login -s 'http:/sales-server:8000' -t Sales -u administrator -p p@ssw0rd!",
       @"tabcmd createproject -n 'Quarterly_Reports' -d 'Workbooks showing quarterly sales reports.'",
       @"abcmd publish 'analysis.twbx' -n 'Sales_Analysis' --db-user 'jsmith' --db-password 'p@ssw0rd'", 
       @"tabcmd refreshextracts workbook 'My Workbook'",
       @"tabcmd logout"

      };


     //Create a File Path

    string BatFile = @"\YourLocation\tabcmd.bat";

    //Create Stream to write to file.

    StreamWriter sWriter = new StreamWriter(BatFile);

     foreach (string cmd in cmdBuilder) 
        { 
          sWriter.WriteLine(cmd); 
        }

       sWriter.Close();

    //Run your Batch File & Remove it when finished.


   Process p = new Process();
   p.StartInfo.CreateNoWindow = true;
   p.StartInfo.UseShellExecute = false;
   p.StartInfo.FileName = "C:\\cmd.exe";
   p.StartInfo.Arguments = @"\YourLocation\tabcmd.bat";
   p.Start();
   p.WaitForExit();
   File.Delete(@"\YourLocation\tabcmd.bat")

我将把输出部分留给你自己。你可以试试这个,它并不完全干净,但会自动执行主要步骤。是这样,还是在最后一个进程退出时为每个命令打开一个进程?

希望对您有所帮助。

【讨论】:

  • 我不明白你在这里建议什么。 p.StartInfo.Arguments = @"\YourLocation\tabcmd.bat";
  • 您构建批处理文件,然后将其保存到某个位置,当您启动进程以运行批处理文件时,参数指向该文件,以便在 cms.exe 启动时运行批处理文件.
  • 如果我手动运行批处理文件,它工作正常,但如果通过代码运行它,什么都不会发生。
  • p.StartInfo.Arguments 不是必需的,只需 p.StartInfo.FileName = @"\YourLocation\tabcmd.bat";够了
猜你喜欢
  • 2020-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-17
  • 2010-09-13
  • 1970-01-01
相关资源
最近更新 更多