Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET File I/O and Registry > Directory.Exists always returns false


Reply
 
Thread Tools Display Modes
  #1  
Old 12-06-2006, 11:45 AM
wensly wensly is offline
Freshman
 
Join Date: May 2003
Location: London, UK
Posts: 36
Default Directory.Exists always returns false


Hi

I am trying to check whether a directory exists, by using the following code:
Code:
Dim bResult As Boolean = Directory.Exists("c:\temp")
MsgBox(bResult)
However, it always returns False even when the directory definitely does exist.

I've had a search around, and I'm thinking it may be something to do with security settings (although it doesn't throw any kind of exception).

Can anyone help please?

Thanks
Wendy
Reply With Quote
  #2  
Old 12-06-2006, 11:51 AM
reboot's Avatar
reboot reboot is offline
Keeper of foo

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Graceland
Posts: 15,612
Default

Works fine for me.
__________________
~ Quod non mortiferum, fortiorem me facit ~

Avatar by lebb
Reply With Quote
  #3  
Old 12-06-2006, 12:16 PM
wensly wensly is offline
Freshman
 
Join Date: May 2003
Location: London, UK
Posts: 36
Default

Hmm, definitely doesn't for me...

I've done some more investigation, and it seems that Directory.Exists may not actually be looking at my c drive at all. If I get a file listing as follows:
Code:
Dim str As String
Dim FileList() As String
 
FileList = Directory.GetFiles("c:", "*")
For i As Integer = 0 To FileList.Length - 1
  str &= FileList(i) & Chr(13)
Next i

MsgBox(str)
...the msgbox returns:
MyProject.exe
MyProject.exe.config
MyProject.pdbc

So, it's doesn't actually seem to be looking at my c: drive at all, but at the project directory.... Directory.Exists("c:") also returns True.

If I use the same code as above, but get it to search in "c:\" (i.e. with a backslash), it raises a security exception:

An unhandled exception of type 'System.Security.SecurityException' occurred in mscorlib.dll

"Additional information: Request for the permission of type System.Security.Permissions.FileIOPermission, mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 failed."

Any ideas?

Thanks
Wendy
Reply With Quote
  #4  
Old 12-07-2006, 06:59 AM
wensly wensly is offline
Freshman
 
Join Date: May 2003
Location: London, UK
Posts: 36
Default

I got some help on this from a colleague, so thought I would post the solution we found. The problem *was* to do with my security settings, and the following steps fixed it:

1. Go to Control Panel | Administrative Tools | Microsoft .NET Framework 1.1 Wizards
2. Click 'Trust an Assembly'
3. Next
4. Add the exe file as a 'manged assembly to trust'
4. Next
5. Selected 'Full Trust'

Directory.Exists then returns the correct result

The only problem with the above is that the exe needs to be re-trusted each time it's compiled, so if anyone has any ideas about how to always trust projects that I create, I'd be interested to hear them.

Thanks
Wendy

Last edited by wensly; 12-07-2006 at 07:05 AM.
Reply With Quote
  #5  
Old 12-07-2006, 07:33 AM
wayneph's Avatar
wayneph wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

Are you creating it on your C:\ drive or is it running from a network location or something like that? I've never had a problem with locally created apps. On the Network, I have set up a Trusted location so that I can create apps in a location that is backed up, and don't run into the Security Exceptions.

Any more information you can provide about your development environment would be appreciated.
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
  #6  
Old 12-07-2006, 07:41 AM
wensly wensly is offline
Freshman
 
Join Date: May 2003
Location: London, UK
Posts: 36
Default

Hi, thanks for your reply.

It's on my local drive in a directory called c:\Projects\MyProjectName, and it gets checked into Source Safe at the end of every day. I know that if I try to save a project to a network location, it tells me that it's not trusted, but this does not happen when I save to c:\Projects.

To be honest, I don't have a clue about permissions / security, so I'm not sure what other options / settings I might need to look at...
Reply With Quote
  #7  
Old 12-07-2006, 08:06 AM
wayneph's Avatar
wayneph wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

What permissions do you have on the machine? Are you an Admin? Are you in the "VS Developers", or "Debugger Users" Groups?

I'm just grasping at straws because I've never had a problem with anything on C.
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
  #8  
Old 12-08-2006, 03:40 AM
wensly wensly is offline
Freshman
 
Join Date: May 2003
Location: London, UK
Posts: 36
Default

I am in the following groups:
Debugger Users
Administrators
Power Users

I switched 'Debugger Users' with 'VS Developers' and re-logged-on; however, that didn't make any difference. I don't seem to be able to assign myself to both the 'Debugger Users' AND 'VS Developers', because it only allows one selection (I am changing my groups via Control Panel | User Accounts | Users tab | Properties | Group Membership tab, which provides a dropdown list to select 'Other' type of user. I'm using Windows XP if that makes a difference).

However, interestingly, the user account for a contractor we employed a while ago is assigned to 'Administrators', 'Debugger Users' and 'VS Developers'. I may be being stupid, but I can't see how this selection can be made via the above interface...
Reply With Quote
  #9  
Old 12-08-2006, 08:15 AM
wayneph's Avatar
wayneph wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

you shouldn't need to be in all of them. But you can get in multiple groups by going into Computer Management, and selecting Local Users and Groups.

(Right click on "My Computer" and go to Manage.)
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
  #10  
Old 12-08-2006, 08:26 AM
wensly wensly is offline
Freshman
 
Join Date: May 2003
Location: London, UK
Posts: 36
Default

Thanks, I've done that, but as (I think) you implied, it doesn't solve the problem...

When I am stepping through the code in debug mode, does it / should it automatically run under my user account? If so, how would I check this?
Reply With Quote
  #11  
Old 12-08-2006, 08:35 AM
wayneph's Avatar
wayneph wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

yes. stepping through code will run with your permissions. unless you're doing some explict Impersonation, but you'd know if that's the case.

If you go into .NET Configuration under administration tools, can you check to see what the trust level is for your computer?

Click on: Configure Code Access Security Policy
Click on: Adjust Zone Security
Select: Make Changes to this Computer, Click Next
Check: "My Computer"

Make sure it is Full Trust.
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
  #12  
Old 12-08-2006, 08:42 AM
wensly wensly is offline
Freshman
 
Join Date: May 2003
Location: London, UK
Posts: 36
Default

Yes, it is Full Trust
Reply With Quote
  #13  
Old 12-08-2006, 08:48 AM
wayneph's Avatar
wayneph wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

Instead of "Make Changes to this Computer", select "Make Changes for the Current User".

Does that have Full Trust as well?

If so, I'm afraid I've run out of things to check... Do you have all of the Famework Service Packs installed?
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
  #14  
Old 12-08-2006, 08:52 AM
wensly wensly is offline
Freshman
 
Join Date: May 2003
Location: London, UK
Posts: 36
Default

Ah-ha! That was set to one level below Full Trust. I've changed it to Full Trust and everything is now working as it should

Many thanks for all your help Wayne.
Reply With Quote
  #15  
Old 12-08-2006, 08:56 AM
wayneph's Avatar
wayneph wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

Good. Because I ran out of other things to look at.

Glad it's working now.
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->