C# windows服务打开网页或者应用程序

来源:互联网 发布:淘宝绿茶哪家好 编辑:程序博客网 时间:2024/05/16 18:11

首先能够打开一个网页或者程序,有以下几种方式

1.使用ie打开网页

System.Diagnostics.Process.Start("iexplore.exe", "http://blog.csdn.net/wuma0q1an");

2.使用系统默认浏览器打开

System.Diagnostics.Process.Start("explorer.exe", "http://blog.csdn.net/wuma0q1an");
<pre code_snippet_id="1710248" snippet_file_name="blog_20160607_4_1801188" name="code" class="csharp">System.Diagnostics.Process.Start("http://blog.csdn.net/wuma0q1an");
3.调用注册表打开

RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"http\shell\open\command\");string s = key.GetValue("").ToString();System.Diagnostics.Process.Start(s.Substring(0, s.Length - 8), "http://blog.csdn.net/wuma0q1an");

4.调用winApi打开,可以打开网页或者程序

ShellExecute(IntPtr.Zero, "open",    @"C:\Users\Administrator\Desktop\Windows服务\ConsoleOpenWeb\ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe",   "", "", ShowCommands.SW_SHOWNORMAL);

        public enum ShowCommands : 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_FORCEMINIMIZE = 11,            SW_MAX = 11        }        [DllImport("shell32.dll")]        static extern IntPtr ShellExecute(            IntPtr hwnd,            string lpOperation,            string lpFile,            string lpParameters,            string lpDirectory,            ShowCommands nShowCmd);

然后在windows服务可以通过调用这些方法来打开,有下面几个注意事项:

1.需要在服务里面开启“允许服务与桌面交互”该选项


2.打开浏览器的话,除了

System.Diagnostics.Process.Start("explorer.exe", "http://blog.csdn.net/wuma0q1an");
这个方法可以正确的调用我默认为chrome浏览器打开之外,其他的皆出现下面这种情况


0 0
原创粉丝点击