Solved

Browse for folder?

Posted on 2004-08-19
9
8,720 Views
Last Modified: 2011-08-18
Hi,

One of my option is to set a path for a specific folder. I am in the process of building an web ui for my server, but I have just realised that I don't know how to call a "Browse for Folder" equivalent as I have in vb6. I want to allow the user to see the server machine drives and or any network drive, but not their own as it would not make senses.

1. Does anyone know how to do this via code i.e. browse for folder dialog box to be called via asp.net
2. Does anyone know any tools that would provide me with this.

I want to stay away from having to download anything, so it would have to be pure asp.net or functionality available via the .net framework as this UI would be mostly to setup the server remotely but likely to be on an intranet and most client would have our windows based application that would be using the .net framework in the first place.

Any coding solution would be greatly appreciated or any tools.

Many thanks.

Thierry
0
Comment
Question by:taf
  • 5
  • 3
9 Comments
 
LVL 28

Accepted Solution

by:
mmarinov earned 500 total points
ID: 11842493
0
 
LVL 4

Expert Comment

by:Sandeepk1999
ID: 11845560
I got this VBScript from somesite(dont remeber which one) while back.. I have tried this and it works. This include the mapped drives

Copy the whole code in the aspx page and run it... Let me know if you have any questions.

Sandeep



<HTML>
      <HEAD>
            <title>WebForm2</title>
            <meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR">
            <meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
            <meta content="JavaScript" name="vs_defaultClientScript">
            <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
            <script language="vbscript">

            Const BIF_returnonlyfsdirs   = &H0001
            Const BIF_dontgobelowdomain  = &H0002
            Const BIF_statustext         = &H0004
            Const BIF_returnfsancestors  = &H0008
            Const BIF_editbox            = &H0010
            Const BIF_validate           = &H0020
            Const BIF_nonewfolder        = &H0200
            Const BIF_browseforcomputer  = &H1000
            Const BIF_browseforprinter   = &H2000
            Const BIF_browseincludefiles = &H4000

            '******************************************************************************
            ' Gordon Ali provided me with the following information:
            ' There is an additional optional parameter.
            ' This fourth parameter gave the following expiremental options.
            ' Any other value starts the function with the desktop, but with My Computer
            ' expanded.
            '
            ' When you pass a textstring instead of the numeric BSF-constants, the root
            ' will be this specific folder or drive.
            '
            '******************************************************************************

            Const BSF_desktop            = 0    'Desktop is the root directory. With BIF_returnonlyfsdirs circumvents problem with OK-button
            Const BSF_internetexplorer   = 1    'Internet Explorer is the root
            Const BSF_programs           = 2    'Programs folder of the start menu is the root
            Const BSF_controlpanel       = 3    'Control Panel is the root. Needs BIF_browseincludefiles
            Const BSF_printers           = 4    'Printers folder is the root. Needs BIF_browseincludefiles
            Const BSF_documents          = 5    'Documentsfolder is the root
            Const BSF_favorites          = 6    'Favorites is the root
            Const BSF_startup            = 7    'Startup-folder of the startmenu is the root. Needs BIF_browseincludefiles
            Const BSF_recent             = 8    'Recentfolder is the root. Needs BIF_browseincludefiles
            Const BSF_sendto             = 9    'Sendto-folder is the root. Needs BIF_browseincludefiles
            Const BSF_recyclebin         = 10   'Recycle Bin is the root. Needs BIF_browseincludefiles
            Const BSF_startmenu          = 11   'Start Menu is the root
            Const BSF_desktopdirectory   = 16   'The Desktopdirectory is the root directory
            Const BSF_drives             = 17   'The drives (My computer) folder is the root
            Const BSF_network            = 18   'The networkneighbourhood is the root
            Const BSF_nethood            = 19   'The nethoodfolder is the root
            Const BSF_fonts              = 20   'The fontsfolder is the root
            Const BSF_templates          = 21   'The templatesfolder is the root
            Const BSF_commonprograms     = 22
            Const BSF_commonstartup      = 23
            Const BSF_commondesktopdir   = 24
            Const BSF_appdata            = 26
            Const BSF_printhood          = 27
            Const BSF_localappdata       = 28
            Const BSF_altstartup         = 29
            Const BSF_commonaltstartup   = 30
            Const BSF_commonfavorites    = 31
            Const BSF_internetcache      = 32
            Const BSF_cookies            = 33
            Const BSF_history            = 34
            Const BSF_commonappdata      = 35
            Const BSF_windows            = 36
            Const BSF_system             = 37
            Const BSF_programfiles       = 38
            Const BSF_mypictures         = 39
            Const BSF_profile            = 40

            Dim strPath
            'strPath = BrowseForFolder("Choose a folder", BIF_editbox + BIF_validate + BIF_browseincludefiles, BSF_desktop)


            'If IsNull(strPath) Then
                  'MsgBox "Invalid Folder Selection"
            'Else
                  'MsgBox strPath
            'End If

            '******************************************************************************
            ' This function displays the "Browse For Folder" dialog and returns a string
            ' with the UNC of the chosen folder. If an invalid folder is selected (such as
            ' "My Computer"), then a null string is returned. It's only argument is the
            ' string for the prompt that appears beneath the title strip.
            '******************************************************************************

            '******************************************************************************
            ' Second parameter - browseinfo - for setting kind of browseactivity
            ' Third parameter - root - this can be a numeric value (see above) or a
            ' textstring (e.g. "c:\program files")
            '******************************************************************************


            Function BrowseForFolder()
                  On Error Resume Next
                  Dim objShell, objFolder, intColonPos, objWshShell, returnerror
                  Dim strPrompt, BrowseInfo, root
                  strPrompt = "Choose File location"
                  BrowseInfo = BIF_editbox + BIF_validate + BIF_browseincludefiles
                  root = BSF_desktop
                  Set objWshShell = CreateObject("WScript.Shell")
                  Set objShell = CreateObject("Shell.Application")


                  '*********************************************************************
                  ' I am unsure of the exact meanings of the hex values here. The first
                  ' one is the handle of the current window and so far as I can tell is
                  ' irrelevant in this case. The second one is the flags property of the
                  ' dialog, and controls some features of its behaviour. 1 seems to
                  ' force some form of validation, so that the OK button is greyed out
                  ' if an invalid folder is selected. I would like to find some
                  ' documentation on this.
                  '*********************************************************************

                  '*********************************************************************
                  '
                  ' The following hexadecimal values are valid for the parameter "browseinfo"
                  ' &H0001      Return only filesearchdirs
                  ' &H0002      Don't go below domain (needed for computersearch)
                  ' &H0004      Includes a status area
                  ' &H0008      Only return file system ancestors
                  ' &H0010      Includes an editbox, so the user can type the name of an item
                  ' &H0020      Validate the name typed in the editbox
                  ' &H1000      Browse for computers
                  ' &H2000      Browse for printers
                  ' &H4000      Browse for everthing (also files)
                  ' These values are documented in the file shlobj.h. This file is part of the Platform SDK
                  ' There should be more values with nice features, but i don't know them yet.
                  '*********************************************************************
                  
                  Set objFolder = objShell.BrowseForFolder(&H0, strPrompt, BrowseInfo, root)
                  
                  BrowseForFolder = objFolder.ParentFolder.ParseName(objFolder.Title).Path

                  '*********************************************************************
                  ' Now handle any errors. I have defined one special case;
                  ' (1) The selected folder is a drive.
                  ' In all other cases return Null.
                  ' When an invalid folder has been selected or the Cancel Button has
                  ' been hit, the function will generate errorcode 424 and
                  ' BrowseForFolder will be set to Null
                  '*********************************************************************
                  
                  returnerror = err.number
                  If returnerror <> 0 Then
                        If returnerror = 424 then
                              BrowseForFolder = Null
                        else
                              'If selected folder is a drive, it will have a colon e.g. C:\

                              intColonPos = InStr(objFolder.Title, ":")

                              If intColonPos > 0 Then
                                    BrowseForFolder = Mid(objFolder.Title, intColonPos - 1, 2) & "\"
                              End If
                        End If
                  End If
            End Function
            </script>
      </HEAD>
      <body MS_POSITIONING="GridLayout">
            <form id="Form1" method="post" runat="server">
                  <BUTTON id="Button1" style="Z-INDEX: 101; LEFT: 311px; POSITION: absolute; TOP: 117px" onclick="TextBox1.Value = BrowseForFolder" type="button" value="Button">
                        Browse</BUTTON>
                  <asp:textbox id="TextBox1" style="Z-INDEX: 102; LEFT: 148px; POSITION: absolute; TOP: 117px" runat="server"></asp:textbox><asp:requiredfieldvalidator id="RequiredFieldValidator1" style="Z-INDEX: 103; LEFT: 389px; POSITION: absolute; TOP: 118px" runat="server" ControlToValidate="TextBox1" ErrorMessage="Choose File(s) location">Choose File(s) location</asp:requiredfieldvalidator>
            </form>
      </body>
</HTML>
0
 

Author Comment

by:taf
ID: 11854816
Hi B..M and Sandeep,

Thanks both for feedback. I have started my own code based on the idea given by B..M, and made it a lot more powerfull and nicer by using a treeview, etc... I have not finished it yet, and I want to have a look at the other suggestion given by Sandeep. So thank you both for getting back to me so promptly.

Sandeep, I have copied your code as you said into an aspx page as you have requested, I got no error but when I click on the button, it simply return a null value in the textbox but no dialogbox showing up?

I am using VB.Net 2003 (upgraded from 2002). Are you using a different version or do you have any other suggestions as to what I can do?

Thanks.

Thierry
0
 
LVL 4

Expert Comment

by:Sandeepk1999
ID: 11854899
I did not include the @Page Directive, so my suggestion would be

Create a new web page
go to HTML view,
Copy the code and paste it from the <HTML> tag.

Let me know.

Sandeep
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 4

Expert Comment

by:Sandeepk1999
ID: 11854966
I tested it in VB.NET 2003, and it works.. as i said copy the code and paste it in your HTML from the <HTML> tag.

Sandeep
0
 

Author Comment

by:taf
ID: 11857002
Hi Sandeep,

I forgot to mention that is was what I did in the first place.

The page is running without errors but when I click on the button, I get a null value returned in the textbox.

Any other ideas?

Thanks.

T.
0
 

Author Comment

by:taf
ID: 11857214
Hi Sandeep,

I have removed the on error "resume next" in the code, and I got a more accurate error:

ActiveX component can't create object: 'WScript.Shell'

I don't believe it is settings in IE 6 as we already use activex components in our products directly in web pages but written in vb6.

Any ideas why?

Thanks.

T.
0
 

Author Comment

by:taf
ID: 11857278
Hi Again,

Sorry I'm a bit quick on the trigger. Everytime I send a comment I suddenly think of trying out something else.

It is down to IE Settings, strangely enough. Under the Security Settings, my default security settings for intranet were modified. I reset it them to default and now I'm getting an another error:

Permission Denied.

I will look into it more.

Thanks.

T.
0
 

Author Comment

by:taf
ID: 12032346
Hi,

Sorry about delay getting back. Anyway, I have given up on the solution trying to access the API via IE, so I will stick to the .NET approach of listing these drives. I will expand on this and include more code to include network drives, and others.

So I'll go with mmarinov's answer, but thanks to the rest of you for all the various feedback.

T.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now