本篇是講述透過網頁(ASP.net)來開啟或關閉Windows Service,原本想用COM來處理,但是覺得太麻煩,網路上找一下果然有類似的文章,就決定來試試。

開發環境
1.開發:VS.net 2010
2.平台:Server 2003 R2+ IIS+ ASP.net 2.0
3.參考:System.ServiceProcess
4.引用網頁:怎样在ASP.NET中调用一个Windows Services?
原本以為將程式寫好就OK,沒想到一直出現Access is denied,弄了半小時改了一堆權限,我連把ASPNET帳戶加入管理者Administrators還是沒辦法取得授權。

後來繼續google一下發現一堆人有相同問題,但是我查到一篇說ASP.net可以在Web.config事先指定使用者,所以在試著在Web.config的<system.web>下加入這行指令,而computername、username、pass請修改為自己的名稱,結果就可以正常執行了。
<identity impersonate="true" userName="computer_name\user_name" password="pass"/>

 


Windows Service就是指下面這東西

image


原始碼開始:----------------------

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<%@ Import Namespace="System.ServiceProcess" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"></head>
<body>
    <form id="form1" runat="server">
    <div>
    <%
        Dim sc As New System.ServiceProcess.ServiceController
        sc.ServiceName = "Fax"     '請輸入正確的Service Name
        'MessageBox.Show(Status.ToString)
        If sc.Status.Equals(ServiceControllerStatus.Stopped) Then
            sc.Start()
            'MessageBox.Show(scStatus.ToString)
        Else
            sc.Stop()
            'MessageBox.Show(scStatus.ToString)
        End If 
        %>
    </div>
    </form>
</body>
</html>

原始碼結束:----------------------

arrow
arrow
    全站熱搜

    夜市 小霸王 發表在 痞客邦 留言(1) 人氣()