转贴:用VS2005(VB.NET )写“思道OA网站配置程序”

Posted on 2008-11-21 12:28  蓝箭GZ  阅读(285)  评论(0编辑  收藏  举报
 

通达OA、华天OAOA系统,在装完主程序后都会弹出一个OA网站配置程序,设置好后确定就可以使用OA系统,很方便,而思道OA暂时还是要手动配置,是否可以用程序配置OA网站呢?答案是肯定的。

 

       主要用到NET类库中的DirectoryEntry,详细介绍参考文章:.Net中如何操作IIS的原理分析

 

摘录片段如下:

DirectoryEntry.Net给我们的一大礼物,他的名字我们就知道他的功能--目录入口。使用过ADSI的人都知道操作IISWinNT这些时,我们还需要提供他们的Path,操作IIS时,这个Path的格式为:

  IIS://ComputerName/Service/Website/Directory

  ComputerName:即操作的服务器的名字,可以是名字也可以是IP,经常用的就是localhost

  Service:即操作的服务器,IIS中有Web,也有FTP,还有SMTP这些服务,我们主要是操作IISWeb功能,因此此处就是"W3SVC",如果是FTP则应是"MSFTPSVC"

  WebSite:一个IIS服务中可以包括很多的站点,这个就用于设置操作的站点。他的值是一个数字,默认是1,表示缺省站点,如果有其它,则从1开始依次类推。

  Directory:不用说,即操作的目录名称,一个站点一般顶层目录为"ROOT",其它目录则是他的孩子(Child)

  首先我们获取一个站点的顶层目录(根目录):

  DirectoryEntry rootfolder = new DirectoryEntry("IIS://localhost/W3SVC/1/ROOT");

  如果我们创建这个对象是没有发生异常,则表示这个目录是真实存在的。

  下面我们来添加新的虚拟目录,比如我们要加的是"Aspcn"

  

      DirectoryEntry newVirDir = rootfolder.Children.Add("Aspcn","IIsWebVirtualDir");

  newVirDir.Invoke("AppCreate",true);

  newVirDir.CommitChanges();

  rootfolder.CommitChanges();

创建目录的思路很简单,即在根目录的子集(rootfolder.Children)中再添加一条记录,这里使用的是 DirectoryEntries类中的Add方法,它返回的是一个DirectoryEntry,表示新加入的目录,第一个参数是虚拟目录的名字,第二个则是Schema的类名以表明我们加入的目录类型。然后再使用DirectoryEntryInvoke方法,调用ADSI中的 "AppCreate"方法将目录真正创建(似乎不走这一步也可以创建目录成功,但是为了保险起见,大家还是用吧),最后便是依次调用新、根目录的 CommitChanges方法,确认此次操作。

在创建新目录时,我们也可以同时给这个目录的属性赋值,但是我的实战经验告诉我,最好不要这样做,如果创建时就赋值,将有很多属性不能赋值成功,比如重要的表示真实目录的Path属性。因此飞刀建议大家最好是先创建目录,然后再赋值,即更新目录信息。

 

程序界面如下:

  

 

程序主要代码如下:

 

1Form1_Load:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

      

'===========暂时没用,以后用在安装包中直接运行这个配置程序===========      

        '取得命令行带的网站目录参数

        Dim mycmd As Array = System.Environment.GetCommandLineArgs

        'MsgBox(mycmd(1))

        'TextBox3.Text = "yoffice.net\webroot"

        Try

            TextBox3.Text = mycmd(1)

        Catch

            'MsgBox("WEB网站目录不正确!", MsgBoxStyle.OkOnly, "提示")

        End Try

'=====================================================================

 

 

        '取得本机的IP地址和机器名

        Dim Address() As System.Net.IPAddress

        Address = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList

        TextBox1.Text = Address(0).ToString()

        Dim myip_xp As String = Address(0).ToString()

        Dim computer_name As String = System.Net.Dns.GetHostName

 

        '先判断网站是否已存在

        Try

            Dim server3 As DirectoryEntry = New DirectoryEntry("IIS://" + TextBox1.Text + "/W3SVC/" + serverID)

 

            '取计算机名称

            ServiceController1.MachineName = computer_name

 

            '取得MSSQLSERVER服务器当前运行状态

            If ServiceController1.Status = 4 Then

                Label5.Text = "服务正在运行"

                Button1.Enabled = False

            End If

            If ServiceController1.Status = 1 Then

                Label5.Text = "服务已停止"

                Button1.Enabled = True

 

            End If

 

        Catch

            MsgBox("OA服务器IP无法连接或OA网站不存在!", MsgBoxStyle.OkOnly, "提示")

            Exit Sub

        End Try

 

    End Sub

 

2Button1_Click

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

 

        ServiceController1.Refresh()

        Label5.Text = "服务正在启动......"

        ServiceController1.Start()

 

        Do While ServiceController1.Status <> 4

            ServiceController1.Refresh()

        Loop

 

        Label5.Text = "服务正在运行"

        Button1.Enabled = False

 

End Sub

 

 

3Button2_Click

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

      

        If TextBox3.Text = "" Then

            MsgBox("请选择OA的安装目录!", MsgBoxStyle.OkOnly, "提示")

            Exit Sub

        End If

 

        Dim mydir As String = TextBox3.Text + "\webroot"

        Dim service As DirectoryEntry

        Dim server As DirectoryEntry

 

        'IP地址格式判断

        If Not Regex.IsMatch(TextBox1.Text, "^([1-9]|[1-9]\d|1\d{2}|2[0-1]\d|22[0-3])(\.(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])){3}$") Then

            MsgBox("IP地址格式不对,请重新输入!", MsgBoxStyle.OkOnly, "提示")

            Exit Sub

        End If

 

        '先测试IP是否通

        If My.Computer.Network.Ping(TextBox1.Text) Then

 

 

            '============创建网站,要求传入OA安装的目录路径====================

 

            '检测输入的端口号是否已在使用

            If IsNumeric(TextBox2.Text) Then

 

                If TextBox2.Text >= 0 And TextBox2.Text <= 65535 Then

                    Dim lngLoop As Long

                    lngLoop = TextBox2.Text

                    Dim myTcpClient As New TcpClient()

 

                    Try

                        myTcpClient.Connect(TextBox1.Text, lngLoop)

                        MsgBox("端口已被使用!", MsgBoxStyle.OkOnly, "提示")

                        myTcpClient.Close()

 

                    Catch ex As SocketException

                        '========================修改端口==============================

                        Dim myport As String = TextBox2.Text

                        Dim myip As String = TextBox1.Text

                        Dim flag1 As String = "0"

                        Try

                            service = New DirectoryEntry("IIS://" + myip + "/W3SVC")

                        Catch

                            MsgBox("OA服务器IP无法连接!", MsgBoxStyle.OkOnly, "提示")

                            Exit Sub

 

                        End Try

 

                        If CheckBox1.CheckState = CheckState.Unchecked Then

 

                            Try

                                server = service.Children.Add(serverID, "IIsWebServer")

 

                            Catch

 

                                '删除旧网站资料

 

                                Dim server8 As DirectoryEntry = New DirectoryEntry("IIS://" + TextBox1.Text + "/W3SVC/" + serverID)

                                service.Children.Remove(server8)

                                server = service.Children.Add(serverID, "IIsWebServer")

 

                            End Try

 

                            '新建思道OA2008网站

                            server.Properties("ServerComment")(0) = "思道OA2008"

                            server.Properties("Serverbindings").Add(myip + ":" + myport + ":")

                            Dim myroot As DirectoryEntry = server.Children.Add("Root", "IIsWebVirtualDir")

                            myroot.Properties("Path")(0) = mydir

                            myroot.Properties("AccessRead")(0) = True

                            myroot.Properties("AccessExecute")(0) = True

                            myroot.Properties("AccessWrite")(0) = False

                            myroot.Properties("ContentIndexed")(0) = False

                            myroot.Properties("EnableDefaultDoc")(0) = True

                            myroot.Properties("EnableDirBrowsing")(0) = False

                            myroot.Properties("AccessScript")(0) = True

                            myroot.Properties("DefaultDoc")(0) = "index.aspx"

                         

                            '创建应用程序池

                            myroot.Invoke("AppCreate", True)

                            myroot.CommitChanges()

                            server.CommitChanges()

                            service.CommitChanges()

                            MsgBox("思道OA2008网站成功创建!", MsgBoxStyle.OkOnly, "提示")

 

                        Else

 

                            '修改XP中的默认网站(serverID=1)属性,新建思道OA2008的虚拟目录

 

                            Dim server_xp As DirectoryEntry = New DirectoryEntry("IIS://" + TextBox1.Text + "/W3SVC/" + serverID)

 

 

                            server_xp.Properties("ServerComment")(0) = "思道OA2008"

                            server_xp.Properties("Serverbindings").Clear()

 

                            server_xp.Properties("Serverbindings").Add(myip + ":" + myport + ":")

                            Dim myroot_xp As DirectoryEntry = server_xp.Children.Find("Root", "IIsWebVirtualDir")

                            myroot_xp.Properties("Path")(0) = mydir

                            myroot_xp.Properties("AccessRead")(0) = True

                            myroot_xp.Properties("AccessExecute")(0) = True

                            myroot_xp.Properties("AccessWrite")(0) = False

                            myroot_xp.Properties("ContentIndexed")(0) = False

                            myroot_xp.Properties("EnableDefaultDoc")(0) = True

                            myroot_xp.Properties("EnableDirBrowsing")(0) = False

                            myroot_xp.Properties("AccessScript")(0) = True

                            myroot_xp.Properties("DefaultDoc")(0) = "index.aspx"

                           

                            '创建应用程序池

                            myroot_xp.Invoke("AppCreate", True)

                            myroot_xp.CommitChanges()

                            server_xp.CommitChanges()

                            service.CommitChanges()

                            MsgBox("思道OA2008网站成功创建!", MsgBoxStyle.OkOnly, "提示")

                        End If

 

                        '数据库服务是否启动

                        If Button1.Enabled = False Then

                            LinkLabel1.Enabled = True

                        Else

                            LinkLabel1.Enabled = False

                        End If

                    End Try

 

 

            Else

                MsgBox("端口号范围:-65535", MsgBoxStyle.OkOnly, "提示")

            End If

        Else

            MsgBox("请输入数字:-65535", MsgBoxStyle.OkOnly, "提示")

        End If

        Else

            MsgBox("OA服务器IP无法连接,请重新输入!", MsgBoxStyle.OkOnly, "提示")

 

        End If

End Sub

 

4Button3_Click

    FolderBrowserDialog1.ShowDialog()

    TextBox3.Text = FolderBrowserDialog1.SelectedPath

 

5LinkLabel2_LinkClicked

    LinkLabel2.LinkVisited = True

    System.Diagnostics.Process.Start("http://www.fg-soft.com")

 

6CheckBox1_CheckedChanged

If CheckBox1.CheckState = CheckState.Checked Then

      

            serverID = "1"              'xp系统

        End If

        If CheckBox1.CheckState = CheckState.Unchecked Then

            serverID = "288888888"      'win2003系统

End If

 

7LinkLabel1_LinkClicked

LinkLabel1.LinkVisited = True

    Dim oaUrl As String = "http://" + TextBox1.Text + ":" + TextBox2.Text

    System.Diagnostics.Process.Start(oaUrl)

    Me.Close()

 

 

8、命名空间:

Imports System.Windows.Forms

Imports System.Management

Imports System.Net

Imports System.Net.Dns

Imports System.Net.Sockets

Imports System.DirectoryServices

Imports System.Text.RegularExpressions

 

 

 

详细源码下载:思道OA网站配置程序

 

 

Copyright © 2024 蓝箭GZ
Powered by .NET 8.0 on Kubernetes