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
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
 
 
-->