Monday, June 6, 2011

Asp.net : Manage web application help in directory structure

In this topic, I will explain how application help can be managed as file system structure. Web application file system is easy to manage and systematic.
In this solution, Application help files can be managed under their respective category and subcategories which will be folders of the file system. This solution will read all directories as category and file as help topic description.



HelpFile Explorer ASPX File

Help Categories

HelpFile Explorer ASPX File
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            'Add a root node
            Dim rootnode As TreeNode = New TreeNode("Help and Support", "Topics")
            rootnode.SelectAction = TreeNodeSelectAction.None
            rootnode.Selected = True
            TreeView1.Nodes.Add(rootnode)
            TreeView1_SelectedNodeChanged(sender, e)
            If Request.QueryString("path") IsNot Nothing Then
                Dim directoryPath() As String = Request.QueryString("path").Split(",")
                Dim dir As String = ""
                Dim valuePath As String = "Topics/"
                For Each dir In directoryPath
                    valuePath += dir
                    If TreeView1.FindNode(valuePath) IsNot Nothing Then
                        TreeView1.FindNode(valuePath).Selected = True
                        TreeView1_SelectedNodeChanged(sender, e)
                        valuePath += "/"
                    End If
                Next
            End If
        End If
    End Sub

    Protected Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.SelectedNodeChanged
        'help files root folder
        Dim appHelpRoot As String = Server.MapPath("~") + "/AppHelp/"
        Dim currentDir As String = appHelpRoot + TreeView1.SelectedNode.ValuePath

        TreeView1.SelectedNode.ChildNodes.Clear()

        Dim dirCollections As String() = Directory.GetDirectories(currentDir)
        If dirCollections.Length > 0 Then
            Dim dir As String = ""
            For Each dir In dirCollections
                Dim curDir As DirectoryInfo = New DirectoryInfo(dir)
                Dim childNode As TreeNode = New TreeNode(curDir.Name, curDir.Name)
                TreeView1.SelectedNode.ChildNodes.Add(childNode)
            Next
        Else
            Dim filePath As String = currentDir + "/help.txt"
            If File.Exists(filePath) Then
                Dim newFile As StreamReader = File.OpenText(filePath)
                showHelpText.Text = newFile.ReadToEnd
                newFile.Close()
            End If
        End If
    End Sub

No comments:

Post a Comment