aptDev01
asked on
Check if Local Directory vb.net
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!
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!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
if it is DriveType.Network. If yes, then its a mapped drive.
Goran
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:\
If checkDrive.Name = "Z:\:"
Should be:
If checkDrive.Name = "Z:\"
The drive name will be the fully escaped drive name, e.g:
C:\
ASKER
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.Dri ves("SOME PATH").DriveType <> IO.DriveType.Fixed Then
'I FREAKED
End If
Thanks!
If My.Computer.FileSystem.Dri
'I FREAKED
End If
Thanks!
z: shouldnt be hardcoded
string dirRoot = System.IO.Directory.GetDir ectoryRoot (directory Path)
string dirRoot = System.IO.Directory.GetDir
Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MsgBox(isFixedDrive("D:\te st").ToStr ing)
End Sub
Function isFixedDrive(ByVal strPath As String) As Boolean
Dim oDir As New System.IO.DirectoryInfo(st rPath)
isFixedDrive = False
Dim drive_info As System.IO.DriveInfo
For Each drive_info In System.IO.DriveInfo.GetDri ves()
If drive_info.Name = oDir.Root.ToString Then
If drive_info.DriveType.ToStr ing = "Fixed" Then
isFixedDrive = True
Exit For
End If
End If
Next
End Function
Regards,
M.Raja
MsgBox(isFixedDrive("D:\te
End Sub
Function isFixedDrive(ByVal strPath As String) As Boolean
Dim oDir As New System.IO.DirectoryInfo(st
isFixedDrive = False
Dim drive_info As System.IO.DriveInfo
For Each drive_info In System.IO.DriveInfo.GetDri
If drive_info.Name = oDir.Root.ToString Then
If drive_info.DriveType.ToStr
isFixedDrive = True
Exit For
End If
End If
Next
End Function
Regards,
M.Raja
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)
{
}
ASKER
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.
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.
and what will you do if I map my drive to T:?
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.
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
ASKER
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.
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.
:) 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).
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).
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.
Just my 2 cents.
Correction
and will always freak out,
should have been
and will never freak out.
and will always freak out,
should have been
and will never freak out.
ASKER
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\Progr ams' 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.
For instance:
1. Share 'C:\Program Files' as 'Programs'
2. Map '\\LOCALCOMPUTERNAME\Progr
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.
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.
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.
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. :)
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. :)
ASKER
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.
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.
>> 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. :)
shoud have been
Ah, sorry, that is incorrect.
Here I am struggling with my english, I speak my native language much better. :)
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.
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.
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.Direct ory.GetDir ectoryRoot (dirPath))
if drive.DriveType = System.IO.DriveType.Networ k then
end if
Dim drive As New System.IO.DriveInfo(System
if drive.DriveType = System.IO.DriveType.Networ
end if
>> 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.
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.
ASKER
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?
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?
My intention was to point out that code should not be hardcoded to Z:. If it is not, then we should close the topic.
Open in new window