473,395 Members | 1,948 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

DirectoryInfo delete and create

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

Mar 16 '07 #1
1 2468
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]
- mv*@spam.guard.caspershouse.com

<or********@gmail.comwrote in message
news:11**********************@n59g2000hsh.googlegr oups.com...
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
Mar 19 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David Turner | last post by:
I have created an ASP.NET page that uses the System.IO.DirectoryInfo.GetFiles() method to get a list of files in a specific directory on our web server. (I simply use this list to build some...
7
by: Charts | last post by:
I login as administrator of the machine. However running the following code get access denied exception for file inside the directory. The source code: DirectoryInfo target = new...
0
by: Glenn Venzke | last post by:
I have an aspx page that is set up to copy backed-up DB files from a shared directory to a local folder. For some reason, it is being denied access to the network share. I have the web app running...
4
by: Rachel | last post by:
I have tried: System.IO.File.Create("C:\FolderName") It does not create a folder. Any ideas without using the File System Object? Rachel
2
by: Tophog | last post by:
I haven't done a lot of file I/O in .NET and thought I had this figured out. I am simply wanting to move a UNC-based directory path to a new path using DirectoryInfo. Example: Source...
4
by: Zeb | last post by:
Hi I'm using DirectoryInfo.Delete so that when a product is removed, all it's associated images (including the folder they sit in) are deleted. This actually seems to work fine. However, the...
0
by: MP | last post by:
Hi, I am getting the Access denied Error on a folder after I try to delete a folder that contains some files through my ASP.NET Application. Following is my code DirectoryInfo directoryInfo...
3
by: Steve Kershaw | last post by:
Hi, I need a way to bind a DirectoryInfo array to a GridView without any errors. The code I'm using to create the DirectoryInfo array is: // Define the current directory DirectoryInfo dir...
5
by: Tom P. | last post by:
I am having the following problem: I create a FileSystemWatcher and wait for events. When the event does happen I refresh a FileSystemInfo list and set properties accordingly (IsFile, IsDir,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.