Forum Moderators: open
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.
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)...
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