无需 Process.start 打开网站
如何在浏览器中不使用 Process.start(...)
打开网站 URL:
System.Diagnostics.Process.Start(@"http://www.google.com");
我无法在以下位置使用 Process.Start()
Windows服务,我不知道为什么。
How to open a website URL in browser without of Process.start(...)
:
System.Diagnostics.Process.Start(@"http://www.google.com");
I can not use Process.Start()
in Windows Service , I do not no know why.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
请参阅问题“Windows 服务如何执行GUI 应用程序?”:
另请注意,您不应该这样做:)
如果您所做的只是启动 URL,则命令可能是
如果您想禁止短暂显示的命令提示符窗口,您可以设置
wShowWindow
字段a href="http://msdn.microsoft.com/en-us/library/ms686331%28v=VS.85%29.aspx" rel="nofollow noreferrer">STARTUPINFO
结构 到SW_HIDE
,或在 .NET 中设置ProcessStartInfo.WindowStyle
属性设置为ProcessWindowStyle.Hidden
。See the answer to the question "How can a Windows service execute a GUI application?":
Note also the opinion that you shouldn't do this :)
If all you are doing is launching a URL, the command might be
If you want to suppress the briefly-displayed Command Prompt window, you can set the
wShowWindow
field of theSTARTUPINFO
structure toSW_HIDE
, or in .NET set theProcessStartInfo.WindowStyle
property toProcessWindowStyle.Hidden
.服务在隔离会话中运行。该会话有自己的桌面,很像登录屏幕。然而,用户永远无法查看它。这是一项非常基本的安全措施,服务通常使用非常特权的帐户运行。您可以使用 Process.Start(),用户将永远无法看到程序的 UI。
这不是一个真正的问题,在服务中启动浏览器是零意义的。
Services run in an isolated session. That session has its own desktop, much like the login screen. A user however can never look at it. This is a very basic security measure, services typically run with a very privileged account. You can use Process.Start(), the user just will never be able to see the UI of the program.
This is not a real problem, it makes zero sense to start a browser in a service.
服务不使用桌面运行,因此我不建议尝试打开浏览器。
如果您只需要相关网站的信息,为了下载和解析信息,您可能需要考虑使用 WebClient 而不是浏览器。这将允许您从任何 Uri 下载,并在服务中解析结果。
Services don't run using the Desktop, so I would not recommend trying to open a browser.
If you just need information from the website in question, in order to download and parse information, you might want to consider using a WebClient instead of a browser. This will allow you to download from any Uri, and parse the results in a service.
Windows 7 上的服务无法以任何方式与桌面交互。
因此,您需要的是一个小进程,它将使用某种与服务通信的方法,为登录的用户启动它并等待服务消息。您可以在启动组或任何其他方式中启动它以达到此目的,只需将其设置得非常小并且不引人注目即可。当然,如果您希望它被注意到,您可以使用托盘图标,甚至某些状态窗口。
服务消息可以像在服务和用户进程之间共享的目录中写入带有 URL 的文件一样简单。这样您就可以获得所需的内容并与大多数 Windows 版本保持兼容。
Services on Windows 7 CAN'T interact with desktop in any way.
So, what you need is a small process that will use some method of communication with the service, start it for the user that is logged on and wait for service message. You can start it in Startup group or any other means for that purpose, just make it very small and unnoticeable. Of course, if you want it to bi noticed, you can use tray icon for it or even some status window.
Service message could be something as simple as writing a file with an URL in the directory that is shared between service and the user process. That way you'll get what you need and stay compatible with most Windows versions.
此处的固定代码: http:// 18and5.blogspot.com/2008/01/i-hope-my-frustration-can-help-someone.html
我必须修复参数处理,以便下面的示例能够实际工作。
这是你必须做的调用:
我已经在我自己的系统上测试了它,它的工作方式就像一个魅力(启用了 UAC 的 Windows 7 x64)
但是我建议创建一个小型存根应用程序,它不会使 cmd 窗口闪烁。并且它将接受 url 作为参数。
另外,您不应该像我在示例中那样使用 cmd.exe 的硬编码路径。不管代码如何工作,我希望其余的应该很清楚:-)
HTH
Fixed code from here: http://18and5.blogspot.com/2008/01/i-hope-my-frustration-can-help-someone.html
I had to fix the parameter handling that the example below will actually work.
And this is the call you have to do:
I have tested it on my own system and it worked like a charm (Windows 7 x64 with enabled UAC)
However I recommend to create a tiny stub application which will not make the cmd window flash. And which will accept the url as parameter.
Plus you should not use the hardcoded path to cmd.exe like I did in the example. However the code works, the rest should be clear I hope :-)
HTH
如果服务必须与用户交互(例如,服务在登录之前启动),则这是一个设计问题。
我通常通过制作一个从用户会话启动的小程序来解决这个问题。如果我必须与服务中的用户交互,它将首先查看用户级程序是否正在运行,如果是,它将向其发送命令。
It is a design problem if a service has to interact with the user (as example, services are started before logon).
I usally solve this problem by making a small program that starts with the user's session. If I have to interact with the user in the service, it will first look if the user level program is running and if it is, it will send commands to it.
如果您对响应感兴趣,请尝试:
If you are interested in the response try:
为什么不尝试模仿在计算机上具有登录权限的用户,仅使用 System.Diagnostics.Process.Start 启动 Web 浏览器的特定代码段。
网页加载后,您到底打算用浏览器做什么?
Why not try to Impersonate a user who has logon privilages on the machine only for that particular piece of code that starts the web browser using System.Diagnostics.Process.Start.
What exactly do you intend to do with browser once the web page gets loaded?
您可以尝试设置“与桌面交互”来运行您的服务。这应该可以解决您的问题,但可能会导致其他问题,例如当有人注销主控制台时您的服务将停止。
You can try to run your service with "interact with desktop" set. This should fix your problem, but may cause other issues like your service will stop when someone logs out of the main console.