Best way to achieve this?

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");
}
}


Answer this question

Best way to achieve this?

  • Douglas H. Troy

    You could use a loop and then use a break statement in the if part of the loop.


  • dr.acv

    I gave it ago and ran into some problems.

    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.

  • wencey

    what/who exactly are you referring to xRuntime :-)

  • bw12117

    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 ;-)

     



  • l Bllizzd l

    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



  • rWarrior

    Ok thanks for the replies, i thought when you return you exited the method.

    Ill have a look at the file watcher.

    Thanks all

  • Phil Rogers

    Watch out though - if the file you want to delete is still in the process of being created when you attempt to delete it, an exception will be thrown.

    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.


  • JRQ

    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:

  • constantly checking to see if a file exists, application would hang (the UI) if it were a WinForms app because if the file doesnt exist, you are constantly checking it until it does exist - this file may not exist until say, tomorrow so its ineffecient really

    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



  • Kate Boothby

    >> 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")



  • Ledeni_Plamen

    "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")"

    Sorry, I thought that the second return would overwrite the first...


  • Wolf5

    James.


  • dvdribeiro

    best practice would be to use a filesystemwatcher component (i've never used it but only read about it)

  • Kapalic

    I'm not sure if this is your problem but I already see a problem.

    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


  • dustinto

    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



  • Best way to achieve this?