Solved

Check if Local Directory vb.net

Posted on 2008-10-07
25
1,259 Views
Last Modified: 2010-04-21
I have an app that handles a silent install script, and I am trying to verify a user's 'Install Path' selection as being a local directory or network. I don't want to allow them to select a mapped drive for installing to.

Example:
1. FolderBrowserDialog opens
2. User selects 'Z:\Programs' as install path
3. App checks if 'Z:\' is local or network path.
4. App alerts user that the path selected in invalid and to try again.

Does anyone know the best way to handle this? I've thought about checking UNC paths and validating on them, but it seems like VB.Net should have some built-in handlers for this type of validation.

Your help is much appreciated!
0
Comment
Question by:aptDev01
  • 13
  • 6
  • 5
  • +1
25 Comments
 
LVL 11

Accepted Solution

by:
ladarling earned 250 total points
ID: 22662623

If My.Computer.FileSystem.Drives(0).DriveType = IO.DriveType.Network Then
'Freak out
End If
0
 
LVL 11

Expert Comment

by:ladarling
ID: 22662726
This might be a little more helpful, in retrospect...
 

  'Replace the reference to Z:\ with a variable containing your drive to check its type...

        Dim myDrives = My.Computer.FileSystem.Drives

        For Each checkDrive As IO.DriveInfo In myDrives

            If checkDrive.Name = "Z:\:" And checkDrive.DriveType = IO.DriveType.Network Then

                'Do something about it.

            End If

        Next

Open in new window

0
 
LVL 18

Expert Comment

by:Priest04
ID: 22662748
Pass the drive letter to System.IO.DriveInfo class's constructor, and then, check the DriveType property to see
if it is DriveType.Network. If yes, then its a mapped drive.

Goran
0
 
LVL 11

Expert Comment

by:ladarling
ID: 22662750
Ooops, typo:
If checkDrive.Name = "Z:\:"
Should be:
If checkDrive.Name = "Z:\"  
The drive name will be the fully escaped drive name, e.g:
  C:\
0
 

Author Closing Comment

by:aptDev01
ID: 31503956
Excellent response. I have found the DriveType method while waiting for a response, but I was returning it to a string rather than comparing the actual types.

If My.Computer.FileSystem.Drives("SOME PATH").DriveType <> IO.DriveType.Fixed Then
'I FREAKED
End If

Thanks!
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22662847
z: shouldnt be hardcoded

string dirRoot = System.IO.Directory.GetDirectoryRoot(directoryPath)
0
 
LVL 14

Expert Comment

by:raja_ind82
ID: 22663062
   Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MsgBox(isFixedDrive("D:\test").ToString)
    End Sub

    Function isFixedDrive(ByVal strPath As String) As Boolean
        Dim oDir As New System.IO.DirectoryInfo(strPath)
        isFixedDrive = False
        Dim drive_info As System.IO.DriveInfo
        For Each drive_info In System.IO.DriveInfo.GetDrives()
            If drive_info.Name = oDir.Root.ToString Then
                If drive_info.DriveType.ToString = "Fixed" Then
                    isFixedDrive = True
                    Exit For
                End If
            End If
        Next
    End Function

Regards,
M.Raja
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22663189
raja_ind82, as said It can be done in two lines, no need to complicate the code



System.IO.DriveInfo drive = new System.IO.DriveInfo(System.IO.Directory.GetDirectoryRoot(dirPath));

if (drive.DriveType == System.IO.DriveType.Network)

{

}

Open in new window

0
 

Author Comment

by:aptDev01
ID: 22663192
Thanks for the continued comments. ladarling's response was all I needed.

raja_ind82, though your solution works, I'm still not a big fan of comparing String values over objects where possible. If you have a good reason for doing so, please feel free to pass it on.
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22663322
and what will you do if I map my drive to T:?
0
 
LVL 11

Expert Comment

by:ladarling
ID: 22663398
Good god, Priest04, the example was just to get him on the trail of the right object reference. I am sure he will change to code to fit his own purposes.
That was just an "example", calm down.
ex·am·ple
Pronunciation: \ig-Èzam-pYl\
Function: noun  
: an instance (as a problem to be solved) serving to illustrate a rule or precept or to act as an exercise in the application of a rule
0
 

Author Comment

by:aptDev01
ID: 22663533
Well, priest04...

The scenario you've presented is, in my own experience, a very uncommon one. If a user has this type of mapped drive and it is selected they will be forced to select the local path, and not the mapped path.

That is to say that I don't find it a common enough scenario to code for checking the UNC path against the local computer name, and then validating accordingly.
0
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

 
LVL 18

Expert Comment

by:Priest04
ID: 22663658
:) Lost nerves, ladarling, altough not sure why. My comments were appropriate, if they botter you then you have a problem with attitude. You should have then go into literature watters, ladarling, since you are better with it than with programming (I dont even know why I am arguing here)

A good programming practice is to avoid hardcoding values. I gave reference, you gave code (not pseudocode). In your first example, you have written a code that would always have false value (unless there is no local hard disk), and will always freak out, as you have posted it. In your second example, you have written a whole procedure,  it would need a couple of letters more to avoid hardcoding Z:, but you diidnt write them, and I dont understand why, if you wrote 100, you could have wrote 110. especially since its a big no no.

If someone points to your code mistakes, you freak out (I am expanding my vocabulary), judging by the way you react (comment).
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22663680
aptDev0, I have seen many network PCs that have both Y: and Z: as mapped drive letters. If your application is for public use, then this setup will not be uncommon, and you should make your code exception free.

Just my 2 cents.
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22663694
Correction

and will always freak out,

should have been

and will never freak out.
0
 

Author Comment

by:aptDev01
ID: 22663788
So, you're saying that a common network configuration is to map a drive to a local share?

For instance:
1. Share 'C:\Program Files' as 'Programs'
2. Map '\\LOCALCOMPUTERNAME\Programs' to Y:
3. Select Y: as the installation path

This, again in my own experience, is NOT a common configuration, and if the user installing the application selects this drive they will know how to change their selection to a proper path.

Of course, if it does become an issue to the public users then I can recall this discussion and code accordingly.
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22663878
No, that is not what I meant. If I understood you correctly, user will select an installation path and you want to make sure that its not a network drive. If this is correct, then drive letters on my compuer could look like this

c:\ (local partition)
d:\ (local partition)
y:\ (network drive \\PC1\folder1
z:\ (network drive \\PC2\folder2

if a user choose y:\somefolder as installation path, then this code would fail.
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22663900
Ah sorry, you are correct. Mapping folders is a a very common configuration.

You cant expect every user on a network to be able to access whole drive, a network administrator would receive a hearth attack, and could freak out. :)
0
 

Author Comment

by:aptDev01
ID: 22663926
Yes, that is correct. I am checking the drive of the selected path to make sure it is Fixed (Local), and not a Network or CD-ROM.

So, this code will fail if any mapped drive, cd-rom, or other external media is selected as the destination.

By fail, we are talking about 'Returning FALSE', not an exception.
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22663929
>> Ah sorry, you are correct.

shoud have been

Ah, sorry, that is incorrect.

Here I am struggling with my english, I speak my native language much better. :)
0
 
LVL 11

Expert Comment

by:ladarling
ID: 22663941
For AptDev01:
This, again in my own experience, is NOT a common configuration, and if the user installing the application selects this drive they will know how to change their selection to a proper path.
I dont see that a lot either. As a matter of fact, I have never seen that configuration and I am one of the net admins for a 200+ seat user base.
For Priest04:
:) Lost nerves, ladarling, altough not sure why. My comments were appropriate...


Your comments were unnecessary, was the whole point.  If you examine my post closely, you *might* realize that the entire point of the code that I posted was to point the author to the DriveInfo class, ...not to tell him how to write his app, good variable naming conventions, the best way to tie his shoes. He understood that, if you read his followup, you apparently skipped over it. You are arguing about situationally irrelevant material, end of story.
..but you diidnt write them, and I dont understand why, if you wrote 100, you could have wrote 110. especially since its a big no no.
 
Oh no, the coding convention police, I didnt realize this was a bust. lol. Get a life.
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22663994
There is a difference between checking if a drive is NOT a network drive or a drive is local drive. In your original question you wanted to make sure that a drive is not a network drive, and I am responding to that request. If you want to check if its a local drive, then its a different story (then there is no Z: involved). Anyway, if you think its ok, then I have no problem with it. If you dont, I gave you code that works, that is, for fixed drive it should be

Dim drive As New System.IO.DriveInfo(System.IO.Directory.GetDirectoryRoot(dirPath))
if drive.DriveType = System.IO.DriveType.Network then

end if
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22664117
>> I dont see that a lot either. As a matter of fact, I have never seen that configuration and I am one of the net admins for a 200+ seat user base.

Either you have something against me, or...

Scenario1: he will install his app in a workgroup environment, and UserA uses data from PC1, and likes music that resides PC2 so he wants to access this share and wil llike to have mapped drive
Scenario2: user often uses FolderZ on the file server, and he wold like to map this folder for faster access, altough he already has mapped him main folder.
Scenario 3: in a big network, there are 2 file servers and user has access to both of them.

Or we should assume that all users dont have rights to map drives, and that there will be Admins that will deny them these rights. Again, assumption is a bad programming policy.

>> If you examine my post closely, you *might* realize that the entire point of the code that I posted was to point the author to the DriveInfo class

You already pointed that in your first post, so why the 2nd?

>> Oh no, the coding convention police, I didnt realize this was a bust. lol. Get a life.

I pointed to a wrong code, and you act like I pulled out your eye, and I should get a life? cccc I will not continue any argument here with you, since you are totaly out of order and have some hate in you that keeps getting out.
0
 

Author Comment

by:aptDev01
ID: 22664227
Priest04,

I am unclear as to the purpose or intentions behind your three scenarios.

I clearly stated that the reason I'm checking if the selected path is network or local is because it is being used as an Installation Path. In other words, it is passing the user selection to an application installer, which, as I'm sure you know, applications should not be installed to network paths.

None of your scenarios relate to the instended purpose of the code I requested. Now, if I had said this was to be used for selecting files that user may be sharing then your point would be valid here, but that is not the case.

Perhaps you can clarify your intentions for all of us?
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22664368
My intention was to point out that code should not be hardcoded to Z:. If it is not, then we should close the topic.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
DataGridView 2 29
Need help with a query. 10 43
Need help with a query 6 54
How can one ComboBox's items be determined by another? 9 22
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

17 Experts available now in Live!

Get 1:1 Help Now