How to delete the file after 30 seconds?

2019-06-14 18:27发布

I am working on a C# project and i need the file to deleted after 30 seconds. So once the file sent to the machine i need the software to count till 30 seconds and lets say.. show a splash form and then delete the file. Please help me out.

So in my case i am copying the file to the bin/debug folder. and after 30 seconds i need to delete the file..

This is my code:

 private void button4_Click(object sender, EventArgs e)
    {
        //string filePath = image_print();
       // MessageBox.Show(filePath, "path");
        string s = image_print() + Print_image();
        if (String.IsNullOrEmpty(s) || img_path.Text == "")
        {
            return;
        }
        else
        {
            PrintFactory.sendTextToLPT1(s);
             //after this the i need the another form to pop  up.. lets say i have a spalsh screen.. and it should show for 30 seconds then i need to delete the file.. where i have codes bwlow

           string Filename = img_path.Text;

           if (string.IsNullOrEmpty(Filename))
           return;

           if (Filename.ToCharArray().Intersect(Path.GetInvalidFileNameChars()).Any())
           return;

           File.Delete(Path.Combine(@"\\bin\Debug", Filename));
        }
    }

    private string image_print()
    {
        OpenFileDialog ofd = new OpenFileDialog();
        string path = "";
        string full_path = "";
        string filename_noext = "";
        ofd.InitialDirectory = @"C:\ZTOOLS\FONTS";
        ofd.Filter = "GRF files (*.grf)|*.grf";
        ofd.FilterIndex = 2;
        ofd.RestoreDirectory = true;
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            filename_noext = System.IO.Path.GetFileName(ofd.FileName);
            path = Path.GetFullPath(ofd.FileName);
            img_path.Text = filename_noext;
            //MessageBox.Show(filename_noext, "Filename"); - - -> switching.grf
            // MessageBox.Show(full_path, "path");
            //move file from location to debug
            string replacepath = @"\\bin\Debug";
            string fileName = System.IO.Path.GetFileName(path);
            string newpath = System.IO.Path.Combine(replacepath, fileName);
           // string newpath = string.Empty;
            if (!System.IO.File.Exists(filename_noext))
                System.IO.File.Copy(path, newpath);
            filename_noext = img_path.Text;
         MessageBox.Show(filename_noext, "path");
        }

        if (string.IsNullOrEmpty(img_path.Text))
            return "";//

        StreamReader test2 = new StreamReader(img_path.Text);
        string s = test2.ReadToEnd();
        return s;
    }


    private string Print_image()
    {
        //some codes
            return s;
    } 

1条回答
小情绪 Triste *
2楼-- · 2019-06-14 19:09

Create a new form to use it as SplashScreen.

In SplashScreen's constructor, take the file path as parameter and also start the timer:

string filePath;

public SplashScreen(string FileToDeletePath)
{
    InitializeComponent();

    this.filePath = FileToDeletePath;

    System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
    timer1.Interval = 3000;
    timer1.Tick += timer1_Tick;
    timer1.Start();
}

void timer1_Tick(object sender, EventArgs e)
{
    //delete file
    if (!string.IsNullOrEmpty(filePath))
        File.Delete(filePath);

    //dispose form after deleting the file
    this.Close();   
}

How to use SplashScreen:

else
{
    PrintFactory.sendTextToLPT1(s);
    //after this the i need the another form to pop  up.. lets say i have a spalsh screen.. and it should show for 30 seconds then i need to delete the file.. where i have codes bwlow

    string Filename = img_path.Text;

    if (string.IsNullOrEmpty(Filename))
        return;

    if (Filename.ToCharArray().Intersect(Path.GetInvalidFileNameChars()).Any())
        return;

    File.Delete(Path.Combine(@"\\bin\Debug", Filename));    //remove this line, it'll be done in SplashScreen

    string filePath = Path.Combine(@"\\bin\Debug", Filename);   //create path of file to be removed

    //SplashScreen
    SplashScreen splash = new SplashScreen(filePath);
    splash.Show();
}

The SplashScreen instance will automatically disposed after deleting the file, and it'll not block your main thread (UI).

查看更多
登录 后发表回答