当前位置: 代码网 > it编程>编程语言>Asp.net > C#运行外部程序的两种方法小结

C#运行外部程序的两种方法小结

2025年12月16日 Asp.net 我要评论
shellexecuteusing system.runtime.interopservices;public enum showwindowcommands : int{ sw_hide =

shellexecute

using system.runtime.interopservices;

public enum showwindowcommands : int
{

    sw_hide = 0,
    sw_shownormal = 1,    //用最近的大小和位置显示,激活
    sw_normal = 1,
    sw_showminimized = 2,
    sw_showmaximized = 3,
    sw_maximize = 3,
    sw_shownoactivate = 4,
    sw_show = 5,
    sw_minimize = 6,
    sw_showminnoactive = 7,
    sw_showna = 8,
    sw_restore = 9,
    sw_showdefault = 10,
    sw_max = 10
}
[dllimport("shell32.dll")]
public static extern intptr shellexecute(
    intptr hwnd,
    string lpszop,
    string lpszfile,
    string lpszparams,
    string lpszdir,
    showwindowcommands fsshowcmd
);

调用

shellexecute(intptr.zero, "open", urlorpath, null, null, showwindowcommands.sw_shownormal);

urlorpath可为exe路径或者网页url(使用默认浏览器打开网页url)

使用ie打开网页url

shellexecute(null, "open", "iexplore", "http://www.csdn.net", null, sw_showmaximized);

process

using system.diagnostics;

private static string execute(string exepath, string parameters)
{
    string result = string.empty;

    using (process p = new process())
    {
        p.startinfo.useshellexecute = false;
        p.startinfo.createnowindow = true;
        p.startinfo.redirectstandardoutput = true;
        p.startinfo.filename = exepath;
        p.startinfo.arguments = parameters;
        p.start();
        p.waitforexit();

        result = p.standardoutput.readtoend();
    }

    return result;
}

到此这篇关于c#运行外部程序的两种方法小结的文章就介绍到这了,更多相关c#运行外部程序内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com