如何在30秒后删除文件?

时间:2014-10-28 21:10:43

标签: c# wpf visual-studio-2010 visual-studio delete-file

我正在开发一个C#项目,我需要在30秒后删除该文件。因此,一旦文件发送到机器,我需要软件计数到30秒,然后让... 显示一个启动表单,然后删除该文件。请帮帮我。

所以在我的情况下,我将文件复制到bin / debug文件夹。 30秒后我需要删除文件..

这是我的代码:

 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 个答案:

答案 0 :(得分:1)

创建一个新表单,将其用作SplashScreen

在SplashScreen的构造函数中,将文件路径作为参数,并启动计时器:

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

如何使用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();
}

SplashScreen实例将在删除文件后自动处理,并且不会阻止您的主线程(UI)。