Forum Moderators: open

Message Too Old, No Replies

DirectoryInfo Class

yay - I'm learning .NET

         

txbakers

3:33 am on Mar 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I got through the first chapter without fuss, but now I'm not having luck with the section on the DirectoryInfo class.

dim dirInfo as DirectoryInfo= new DirectoryInfo(tDnme.text)

This should throw an exception when I put a bogus name in the text box, and should proceed when I put a real directory. However, it doesn't. it always shows stuff regardless of whether the directory exists.

Am I doing something wrong here?

thanks for bearing with me as I learn this.

Easy_Coder

1:14 am on Mar 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



txbakers -

The DirectoryInfo(tDnme.text) constructor is basically taking whatever you give it and is shoving that value into these public properties:

public string Name { virtual get; }
public string FullName { virtual get; }

And if the the directory does not exist you can expect this property to be set to false:
public bool Exists { virtual get; }

So as long as you give the constructor a string it is fat and happy.

Check the Exists propery, what are you seeing?
if (dirInfo.Exists == false)...

txbakers

1:35 am on Mar 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the tip - I'll try that. the book didn't mention that technique.... All it said was the program would throw an exception if it didn't find the directory.

Easy_Coder

3:14 am on Mar 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I looked at the entire public interface and then wired up some quick code. In debug mode (f11) you can step and see what's going on line by line in the Autos Window.

From a console app I did this (no exceptions are thrown):
static void Main(string[] args)
{
DirectoryInfo d = new DirectoryInfo(@"c:\ttt\");
if (d.Exists == false)
{
Console.WriteLine("Directory is not available");
}
else
{
Console.WriteLine("Directory found");
}
}

Here's the public interface for DirectoryInfo; you can get these from Wincv.exe (search your machine for it of you have vs.net):

public sealed class System.IO.DirectoryInfo :
System.IO.FileSystemInfo,
System.Runtime.Serialization.ISerializable
{

// Fields

// Constructors
public DirectoryInfo(string path);

// Properties
public FileAttributes Attributes { get; set; }
public DateTime CreationTime { get; set; }
public DateTime CreationTimeUtc { get; set; }
public bool Exists { virtual get; }
public string Extension { get; }
public string FullName { virtual get; }
public DateTime LastAccessTime { get; set; }
public DateTime LastAccessTimeUtc { get; set; }
public DateTime LastWriteTime { get; set; }
public DateTime LastWriteTimeUtc { get; set; }
public string Name { virtual get; }
public DirectoryInfo Parent { get; }
public DirectoryInfo Root { get; }

// Methods
public void Create();
public virtual System.Runtime.Remoting.ObjRef CreateObjRef(Type requestedType);
public System.IO.DirectoryInfo CreateSubdirectory(string path);
public virtual void Delete();
public void Delete(bool recursive);
public virtual bool Equals(object obj);
public System.IO.DirectoryInfo[] GetDirectories();
public System.IO.DirectoryInfo[] GetDirectories(string searchPattern);
public System.IO.FileInfo[] GetFiles();
public System.IO.FileInfo[] GetFiles(string searchPattern);
public System.IO.FileSystemInfo[] GetFileSystemInfos();
public System.IO.FileSystemInfo[] GetFileSystemInfos(string searchPattern);
public virtual int GetHashCode();
public virtual object GetLifetimeService();
public virtual void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
public Type GetType();
public virtual object InitializeLifetimeService();
public void MoveTo(string destDirName);
public void Refresh();
public virtual string ToString();
} // end of System.IO.DirectoryInfo

txbakers

3:42 am on Mar 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks again! I'm not using any of the IDE programs - doing it all by hand in TextPad.

I want to learn the code first before dealing with the Drop and Drag stuff. I'm so comfortable just writing it, I can't bear to go back to the gui.

I'm about to try the .Exists now.