zoukankan      html  css  js  c++  java
  • How to Execute a Command via C#

    Introduction

    It is normal practice to open the Windows command prompt and execute commands. The command when executed shows the result onto the screen. There are many commands that we execute daily such as dir, find, etc. A situation may arise when you want to execute a (shell) command from the C# application.

    Don't worry!!! Here is the code to do so…

    Using the Code

    The code given below creates a process i.e. a command process and then invokes the command that we want to execute. The result of the command is stored in a string variable, which can then be used for further reference. The command execution can happen in two ways, synchronously and asynchronously. In the asynchronous command execution, we just invoke the command execution using a thread that runs independently. The code has enough comments, hence making it self-explanatory.

    Below is the code to execute the command synchronously:

    Below is the code to execute the command asynchronously:

    If we observe carefully, the asynchronous execution of the command actually invokes the synchronous command execution method using a thread. The thread runs in the background making the command execution asynchronous in nature.

    In the above execution sample, we find that there are two result sets of the command "dir". The first one appears immediately after the command and the second appears after the "Done!" statement. In this case, the first one is the synchronous execution of the command, which happens immediately and the second is the asynchronous execution of the "dir" command.

  • 相关阅读:
    Ant学习-002-ant 执行 TestNG 测试用例时 [testng] java.lang.NoClassDefFoundError: com/beust/jcommander/ParameterException 解决方案
    Ant学习-001-ant 基础知识及windows环境配置
    Selenium2学习-032-WebUI自动化实战实例-030-JavaScript 在 Selenium 自动化中的应用实例之五(高亮标示元素)
    Selenium2学习-031-WebUI自动化实战实例-029-JavaScript 在 Selenium 自动化中的应用实例之四(获取元素位置和大小)
    Selenium2学习-030-WebUI自动化实战实例-028-获取元素位置及大小
    Selenium2学习-029-WebUI自动化实战实例-027-判断元素是否存在
    Selenium2学习-028-WebUI自动化实战实例-026-获取页面元素值或者元素属性值
    Selenium2学习-027-WebUI自动化实战实例-025-JavaScript 在 Selenium 自动化中的应用实例之三(页面滚屏,模拟鼠标拖动滚动条)
    Selenium2学习-026-WebUI自动化实战实例-024-获取页面元素
    Selenium2学习-025-WebUI自动化实战实例-023-页面快照截图应用之一 -- 常规截图(全页面)
  • 原文地址:https://www.cnblogs.com/Blackeye286/p/3210810.html
Copyright © 2011-2022 走看看