How to open UNC directory which residing into network shared location from aspx page

Raki 486 Reputation points
2023-04-14T18:45:35.2066667+00:00

Hello, i am developing a web application where i want to allow users open a directory via button click. my directory path in the network shared location (UNC Path) like below not on the web server itself. \\azurefile1\hdcshared\Benedict Insurance Letters\EmailLetters\Original\Requirements So Basically there will be a button on the aspx page and when user click the button all the files inside the directory should display on windows explorer. my below code is working when i am running the application on my local Desktop but not working when application residing on the web server and accessing from the client machine. any idea how to achieve this functionality? is it possible using client side JavaScript? User's image

Microsoft 365 and Office | Development | Office JavaScript API
Developer technologies | ASP.NET | Other
Developer technologies | C#
{count} votes

2 answers

Sort by: Most helpful
  1. VasimTamboli 5,215 Reputation points
    2023-04-14T20:35:58.9933333+00:00

    It is not possible to directly open a UNC path from a web application due to security restrictions. Browsers do not allow access to the local file system for security reasons.

    One solution would be to create a network share on the web server that points to the UNC path and then use server-side code to read the contents of the directory and return it to the client. You can use ASP.NET or any other server-side scripting language to achieve this.

    Here is an example of how to read the contents of a directory in ASP.NET using C#:

    string directoryPath = @"\azurefile1\hdcshared\Benedict Insurance Letters\EmailLetters\Original\Requirements"; string[] files = Directory.GetFiles(directoryPath);

    This code will return an array of file names in the specified directory. You can then use this information to display a list of files to the user.

    Alternatively, you can use a third-party file manager like FileRun or FileManager to achieve this functionality. These tools have a web interface and can be configured to access files on a network share.

    3 people found this answer helpful.

  2. Albert Kallal 5,586 Reputation points
    2023-04-15T18:29:29.03+00:00

    As others pointed out, you can't launch a windows file explorer on the "client" side computer. As pointed out, when you launch a windows file explorer, it launches on the server side - not the users computer (running a browser). However, you can certainly build your own "file" page in asp.net, and "display" the files in a gridview, or listview. And, you can even say drop in a treeview, and "fake" a file like explorer. So, say we drop in a treeview control into our page. So, we have this markup (I used the "auto format", so it will setup the icons automatic for you. So, this markup:

        <h3 id="fileHead" runat="server"></h3>
        <asp:TreeView ID="TreeView1" runat="server" ImageSet="XPFileExplorer" 
            NodeIndent="15" OnTreeNodeExpanded="TreeView1_TreeNodeExpanded1" >
            <HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
            <NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black"
                HorizontalPadding="2px" NodeSpacing="0px" VerticalPadding="2px" />
            <ParentNodeStyle Font-Bold="False" />
            <SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" 
                HorizontalPadding="0px" VerticalPadding="0px" />
        </asp:TreeView>
    
    

    So, I just dragged + dropped in a treeview. And the code is quite easy - and it not even recursive (it does not help for this type of problem). So, my code looks like this:

        Dim sRoot As String = "c:\SERVER01"
    
        Public Class MyFolder
            Public MyFiles As FileInfo()
            Public MyFolders As DirectoryInfo()
        End Class
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            If Not IsPostBack Then
                fileHead.InnerText = sRoot
                Dim MyFiles As MyFolder = GetFiles(sRoot)
                LoadTreeFiles(sRoot, MyFiles, "", Nothing)
            End If
    
        End Sub
    
    

    So, above is the page load. And then the additional code on that page is this:

        Public Function GetFiles(sRootFolder) As MyFolder
    
            Dim MyDir As New DirectoryInfo(sRootFolder)
            Dim cMyFolder As New MyFolder
    
            cMyFolder.MyFolders = MyDir.GetDirectories("*.*", SearchOption.TopDirectoryOnly)
            cMyFolder.MyFiles = MyDir.GetFiles("*.*", SearchOption.TopDirectoryOnly)
    
            Return cMyFolder
    
        End Function
    
    
        Sub LoadTreeFiles(fRootL As String,
                          lParent As MyFolder,
                          sParentID As String,
                          tTreeNode As TreeNode)
    
            ' add folders if any
            For Each sFolder As DirectoryInfo In lParent.MyFolders
                Dim child As New TreeNode
                child.Text = sFolder.Name
                child.Value = sFolder.FullName
                child.Expanded = False
                child.PopulateOnDemand = True
                child.ShowCheckBox = False
    
                If sParentID = "" Then
                    TreeView1.Nodes.Add(child)
                Else
                    tTreeNode.ChildNodes.Add(child)
                End If
            Next
            ' now any files
            For Each sFile As FileInfo In lParent.MyFiles
    
                Dim child As New TreeNode
                child.Text = sFile.Name
                child.Value = sFile.FullName
                child.Expanded = False
                child.ShowCheckBox = True
                child.PopulateOnDemand = False
    
                If sParentID = "" Then
                    TreeView1.Nodes.Add(child)
                Else
                    tTreeNode.ChildNodes.Add(child)
                End If
            Next
    
        End Sub
    
        Protected Sub TreeView1_TreeNodeExpanded1(sender As Object, e As TreeNodeEventArgs) Handles TreeView1.TreeNodeExpanded
    
            Dim child As TreeNode = e.Node
            Dim dtChild As MyFolder = GetFiles(child.Value)
    
            LoadTreeFiles(child.Value, dtChild, child.Text, child)
    
        End Sub
    
    
    

    Now, about is the "max" amount of code I would dare post here. Not too much, and apologies if this posting has too much code. So, now, the result looks like this: treeviewf

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.