首页 > 编程 > .NET > 正文

如何在.net 中执行console命令

2024-07-10 12:58:55
字体:
来源:转载
供稿:网友


如何在.net 中执行console命令private sub form1_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load        '创建一个新的进程对象
        dim mycmdprocess as new process
        '注册进程退出事件
        'mycmdprocess.exited += new system.eventhandler(mycmdprocess_exited)
        addhandler mycmdprocess.exited, addressof mycmdprocess_exited
         mycmdprocess.startinfo.filename = "cmd"       '要执行的命令
        '将参数传给要调用的应用程序 /c 执行字符串指定的命令然后终断 ,调用dir,同时将结果输出到应用程序文件夹下test.txt.
        mycmdprocess.startinfo.arguments = "/c  dir >test.txt"
        mycmdprocess.startinfo.redirectstandardoutput = true
        mycmdprocess.startinfo.useshellexecute = false
        mycmdprocess.startinfo.createnowindow = true
        mycmdprocess.enableraisingevents = true
        mycmdprocess.start()
        console.read()
    end sub '进程退出时调用的方法private sub mycmdprocess_exited(byval sender as object, byval e as system.eventargs)
        try
            dim myfile as system.io.streamreader = new system.io.streamreader("test.txt")
            dim mystring as string = myfile.readtoend()
            myfile.close()
            messagebox.show(mystring)
        catch ex as exception
            messagebox.show(ex.message)
        end try
    end sub
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表