I need to run a loop to check if a file exists then delete it. Once its deleted it can stop looking. However, at the time the loop starts it may not exist, but it will evntually be created.
do
{
delete();
Console.WriteLine("Checking");
} while (check() == false);
private static bool check()
{
if (File.Exists(Environment.GetEnvironmentVariable("SystemRoot") + @"\myfile.dat"))
return true;
return false;
}
public static void delete()
{
if (File.Exists(Environment.GetEnvironmentVariable("SystemRoot") + @"\myfile.dat"))
{
File.Delete(Environment.GetEnvironmentVariable("SystemRoot") + @"\myfile.dat");
Console.WriteLine("Deleted");
}
}

Best way to achieve this?
BeaverMan
here is one way of doing it:
while (!Check())
{
Console.WriteLine("Checking...");
}
this.Delete();
private static bool Check()
{
if (File.Exists(Environment.GetEnvironmentVariable("SystemRoot") + @"\myfile.dat"))
{
return true;
}
else
{
return false;
}
}
public static void delete()
{
if (File.Exists(Environment.GetEnvironmentVariable("SystemRoot") + @"\myfile.dat"))
{
File.Delete(Environment.GetEnvironmentVariable("SystemRoot") + @"\myfile.dat");
Console.WriteLine("Deleted");
}
}
this could be a problem:
What exactly are you trying to achieve or rather when I know you are checking for a file but why check it in a loop until it exists Perhaps there is another way we can help you if you can tell us more in depth about what you are trying to achieve
Rajesh batchu
hasfad
take a look at this, more about the FSW class:
http://msdn2.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx
Hopefully the examples provided will give you a clearer vision!
edit: looks like Andrej also provided the link ;-)
Tiago Dá Mesquita
jortiz
modtran
Ill have a look at the file watcher.
Thanks all
James_Steven
If your application is just waiting for a file to appear so it could delete it, maybe the FileSystemWatcher class could help you here It helps you monitor the file system for changes, for example - it can notify you when a specific file appears in a specific location...
Andrej
samssb
private void mainForm_Load(object sender, EventArgs e)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = Environment.GetEnvironmentVariable("SystemRoot")";
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName;
watcher.Filter = "file.dat";
watcher.IncludeSubdirectories = false;
//watcher.InternalBufferSize = 32384;
Console.Out.WriteLine(watcher.InternalBufferSize.ToString());
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
}
private static void OnChanged(object source, FileSystemEventArgs e)
{
Console.Out.WriteLine(e.FullPath.ToString());
File.Delete(e.FullPath);
}
Problems:
If the file exists when the program starts its not deleted (as its only detecting changes, simpile if file exists delete should fix it )
If the file is found as soon as its created i get the error file is in use.
ameyayashu
You'd have to:
* In a loop, wait for the file to appear (or use FileSystemWatcher)
* In a loop, keep trying to delete the file until it is deleted (or the loop has run a certain number of times, in which case an error should be returned or displayed).
In both loops, you MUST put a short pause in (Sleep(200) or somesuch) otherwise you'll hammer the CPU/disk and the system will be sluggish.
Antoni Pérez
Zadoras
>> If you dont include braces in an if statement, the first line is executed while the condition is true. yes that is all well and good, but if it is true, it will return true, and then move on to also return false
Therefore it will return two values...thats not good <<
That's just plain wrong. When the first return is reached, execution of the method stops, and the one value is returned. A method cannot return two values in this way (as in it would violate a law of phyisics) (And yes, there are ways that a method could return two values -- basically by stuffing two values into one object --- which is why I stated "in this way")
Alexei_shk
Sorry, I thought that the second return would overwrite the first...
.NETPhreak
For the first question - yes, you could check if file exists when application starts.
For the second one, look for Matthew's pointers a few posts up; you have to wait till file is released. If it's a large file, writing could take a bit longer.
Andrej
simon_
If you dont include braces in an if statement, the first line is executed while the condition is true. yes that is all well and good, but if it is true, it will return true, and then move on to also return false
Therefore it will return two values...thats not good
thus you must use an else statement for the return false part