DirectoryInfo delete and create

  • Thread starter Thread starter oren.almog
  • Start date Start date
O

oren.almog

Hi

I am making my first steps with C# and I don´t have much programming
experience so this might be trivial.

Here is the code:

class Program
{
static void Main(string[] args)
{
DirectoryInfo myDir = new DirectoryInfo("c:\\mydir");
if ((myDir.Exists)) {
myDir.Delete();
}

Console.WriteLine(myDir.Exists);
Console.ReadLine();
}
}

I tried this both with deleting and creating the directory. What
happens is that when the if clause gets executed (directory exists so
it is than deleted), the myDir.Exists property is not updated (it will
still hold true even if the directory was deleted in the line above).
Is this the way it is supposed to work?

Thanks Ahead
Oren
 
Oren,

Yes, this behavior is expected. If you wish to refresh the data in the
object to reflect the current state, then you have to call the Refresh
method on the DirectoryInfo instance.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hi

I am making my first steps with C# and I don´t have much programming
experience so this might be trivial.

Here is the code:

class Program
{
static void Main(string[] args)
{
DirectoryInfo myDir = new DirectoryInfo("c:\\mydir");
if ((myDir.Exists)) {
myDir.Delete();
}

Console.WriteLine(myDir.Exists);
Console.ReadLine();
}
}

I tried this both with deleting and creating the directory. What
happens is that when the if clause gets executed (directory exists so
it is than deleted), the myDir.Exists property is not updated (it will
still hold true even if the directory was deleted in the line above).
Is this the way it is supposed to work?

Thanks Ahead
Oren
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top