• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1191
  • Last Modified:

Set a folder to not view hidden files in VB.NET

This question is similar to asked a few years ago by adinas regarding VB 6 (http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20102004.html).  Hopefully the solution is a bit simpler with the advent of the .NET Framework.

I've created a VB.NET application with 47 strongly-typed Crystal reports embedded in it.  I've recently added logic to it that will allow certain users access to only 9 of those reports by hiding the other 38 (changing the file attribute to Hidden).  However, even if files are hidden in a directory, unless the folder option has been set to not view hidden files (Tools -> Folder Options -> View -> Files and Folders -> Hidden Files and Folders -> "Do not show hidden files and folders"), the files can still be seen (translucent) and run by the user in the Open File dialog.  I need to find the object reference used to expose the Tools/Folder Options methods and properties.  Does anyone know what and where that is, or am I stuck with modifying the users' Windows registry (gasp!) programatically?  I've looked everywhere for an answer to no avail, so I found myself signing up for Premium service here.

P.S.: we're a .NET Framework shop with most of our users running XP, but a few stragglers are still using 2000 or, possibly, 98 (gasp! gasp!)

Thanks in advance,

workin' class joe
0
workinclassjoe
Asked:
workinclassjoe
  • 6
  • 5
1 Solution
 
Naveen SwamyCommented:
see for this you need to intercept the os messages or hook/subclass the dialog window or easy way you could lock all other files while the current file is open

if you were able to do this, it would affect windows explorer as well.  
 
The setting is in the registry:  
 
HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\HideFileExt  
 

for locking :
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21161681.html
http://www.experts-exchange.com/Programming/Q_21618669.html


0
 
sr101880Commented:
If you have the reports embedded in your application, why are your users accessing the reports from the open file dialog?  Did you distribute your reports with your app as well as embedding them?  If so, you need to remove the reports from your setup package and point the report viewer to your embedded reports.  Then I would launch your reports based on user from a custom form.  Let me know if I am off in left field or if you would like I can walk you through this step by step.
0
 
workinclassjoeAuthor Commented:
To navstar16:
That's great, but:
1) how do I apply this to only a specific folder, namely, the folder the reports are in?
2) how do I do so programmatically?
3) is this the same entry for EVERY version of Windows? Like I said, some of our users may be on 2000 or 98.
4) I think you meant the entry above HideFileExt called Hidden, did you not? I'm not altogether comfortable with messin' around in the Registry anyway, so I really need to make sure I'm only changing what needs to be changed.

To sr101880:
Yes, the reports are distributed with the app.  The class library I developed consists of a form with a Crystal Report Viewer in it.  Above the viewer are a drop-down list on the right and a "Choose Report" button which instantiates an OpenFile dialog on the left.  The drop-down list is a resource from which the user can select from one of up to 30 reports in the report folder.  I limit the list to 30 for the obvious cosmetic reason: keeps the form clean, doesn't overwhelm the user.  Of course, if there are more than 30 reports to choose from the user can use the "Choose Report" button to see them all.

But, now that I think of it and based on your comment, I'm gathering the list I use to populate the drop-down from the Directory.GetFiles method, like so:

        Dim dirfiles As String() = Directory.GetFiles(strMainRptsDir, "*.rpt")
        .
        .
        .
            z = 0
            For Each dirfile In dirfiles
                If File.GetAttributes(dirfile) <> FileAttributes.Hidden Then
                    dirfile = Microsoft.VisualBasic.Right(dirfile, dirfile.Length - strMainRptsDir.Length)
                    dirfile = Microsoft.VisualBasic.Left(dirfile, dirfile.Length - 4)
                    dirfiles.SetValue(dirfile, z) 'left over prep for old statement "cbxSelectReportAlt.Items.AddRange(dirfiles)"
                Else
                    dirfiles.SetValue(Nothing, z) 'before the need to skip hidden files
                End If
                z = z + 1
            Next
        .
        .
        .
            For Each dirfile In dirfiles
               If dirfile <> Nothing Then
                   cbxSelectReportAlt.Items.Add(dirfile)
               End If
            Next

...so I really don't need to embed the reports in my app at all, do I?  Perhaps I'm using the wrong choice of words. The reports are distributed with the app, but the viewer doesn't point directly to any single report; it's part of a class library, so I don't want it to. The user chooses a report, the report name goes to ReportViewer.ReportSource, ba da bing, displayed. I don't think my reports are "embedded" as I previously lead on. Is that what you're getting at?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
sr101880Commented:
I was thinking just the opposite.    I would remove the distributed reports from your setup package.  That way your users can't get to them from the file system.  I bet you have both embeded and distributed reports in your program.  If you have embedded reports you don't have to mess with the os.  All you need to do is setup your choose report button to populate a listbox or something similar with the reports you want each user to have access too.
0
 
sr101880Commented:


If you brought up another form to load the reports, it would look something like this:

Dim Receipt As New rptReceipt  '<--------------  This is your report
Dim ReportViewer As New frmReportView  '<---------------  This is the form your viewer is located on.

Receipt.SetDataSource(RegularIntake.cDS)   '<------- This sets the data source for your report in this case it is a dataset

ReportViewer.crvReportView.ReportSource = Receipt  '<----------- This sets the report source for the viewer on the viewer form

ReportViewer.ShowDialog()

I would create a sub using the code above to load the reports by the name selected.

Let me know if you have any questions.


0
 
workinclassjoeAuthor Commented:
Thanks, sr101880, for all your help.  Turns out, after taking another look with one of my more experienced teammates, I don't have embedded reports after all.  Everything regarding Crystal Reports is distributed; I'm just pulling the reports into the viewer by name (CrystalReportViewer1.ReportSource = "C:\xyz\pdq\abcdef.rpt").  To be embedded or strongly typed or whatever would mean that each report would have it's own ReportDocument object defined somewhere in a class, which they don't.

This project seems to have dragged on forever due to pure tedium, present subject included.  I think I've found a solution that'll wrap things up pretty quickly.  If I set the Folder Options of the project's Crystal Reports folder to "Do not show hidden files and folders", the attribute seems to carry over to the folder for the install app as well.  If this proves successful I'll make it a part of my install routine, otherwise I'll give the code above a try.

Thanks for all your help, really appreciate it.
0
 
sr101880Commented:
Your welcome!  Let me know if you get stuck on anything. :-)
0
 
workinclassjoeAuthor Commented:
Oh! I didn't know this thread was still open.  I thought it automatically closed when I assigned points.  I’m new at this portal.

I was able to further enhance the solution above by finding the registry key that controls Hidden Files and Folders visibility, \HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\Hidden, and changing it accordingly (1 = show, 2 = do not show).

The code’s included below.  I pulled this together in Notepad so some of the indentions are missing, but it should still be usable give or take a few syntax burps.  By default VS .NET beautifies most of this for you anyway.

Hope this helps someone out there.

Dave

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

imports system
Imports Microsoft.Win32
.
.
.
Private OldintHideValue As Integer = 0 'class-wide variable
.
.
.

' Create a RegistryKey instance, which will access the HKEY_CURRENT_USER
' key in the registry of this machine and retrieve the
' Folder View Options value
Dim regHideValue As RegistryKey
Dim intHideValue As Integer = 0


regHideValue =
Registry.CurrentUser.OpenSubKey("\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Advanced", True)

'Set the RegistryKey value to 2 (Do not show hidden files).
'According to the help text, the method Registry.CurrentUser points to
'the HKCU base key on the latest Windows versions (Windows 98, Windows NT 4.0, 'Windows Millennium Edition, 'Windows 2000, Windows XP Home Edition, 'Windows XP Professional, Windows Server 2003 family), so this code 'should work on all of them.
If Not( regHideValue Is Nothing) Then
    OldintHideValue = regHideValue.GetValue("Hidden", 0)
    intHideValue = 2
    regHideValue.SetValue("Hidden", intHideValue)
    regHideValue.Close()
End If

.
.
.
.
.
.

' Put the value of "Folder View Options" back the way the user had it.
Dim regHideValue As RegistryKey
'''Dim intHideValue As Integer = 0


regHideValue =
Registry.CurrentUser.OpenSubKey("\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Advanced", True)

If (Not regHideValue Is Nothing) Then
    regHideValue.SetValue("Hidden", oldintHideValue)
    regHideValue.Close()
end if
0
 
sr101880Commented:
Great work this looks very simple to use!  Does this work for your 98 box?
0
 
workinclassjoeAuthor Commented:
Don't know, haven't tried it on one yet.  I'm building an install for the application now.  As soon as it finishes, I'm going to Hardware Support to get a laptop with 98 installed.  I'll let you know how it turns out.
0
 
sr101880Commented:
Thanks!
0
 
workinclassjoeAuthor Commented:
Hey sr101880,

It worked beautifully!  Outside of having to download .Net Framework 1.1 and 2.0, no hiccups, no problems.

Sorry for taking so long to reply.  Got kinda bogged down with other stuff, and this topic closed on me due to inactivity.  I had it reopened for your requested reply.

Thanks again for all your help,

Dave
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now