如何在30秒后删除文件/或在作业完成后删除一次文件

本文关键字:文件 删除 一次 30秒 作业 | 更新日期: 2023-09-27 18:28:16

我正在处理一个C#项目,我需要在30秒后删除该文件。因此,一旦文件发送到机器,我需要软件计数到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
        {
         //here its coming to the splash screen code, But data is transferred to the machine only after the splash screen is close :-(
           this.Hide();
        omg = new image();
        omg.ShowDialog();
        this.Show();
         //splash screen closed and then data is transferred.. which i don't need.. i need simultaneous job to be done at the same time..
         PrintFactory.sendTextToLPT1(s);
        }
    }
    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;
    } 

图像形式中:我有以下代码

public partial class image : Form
{
    string filePath;
    public image()
    {
        InitializeComponent();
       // this.filePath = FileToDeletePath;
        System.Timers.Timer timer1 = new System.Timers.Timer();
        timer1.Interval = 30000;
        timer1.Elapsed += timer1_Elapsed;
        timer1.Start();
    }
    private void image_Load(object sender, EventArgs e)
    {
    }
    void timer1_Elapsed(object sender, ElapsedEventArgs e)
    {
        //delete the file using "filePath"
        string Filename = img_path.Text; // here i cannot pass the old string file name with  extension to this form.. Any ways please help me out
        if (string.IsNullOrEmpty(Filename))
            return;
        if (Filename.ToCharArray().Intersect(Path.GetInvalidFileNameChars()).Any())
            return;
        File.Delete(Path.Combine(@"''bin'Debug", Filename));
    }
}

如何在30秒后删除文件/或在作业完成后删除一次文件

类似这样的东西????

Task waitfordelete = Task.Run(() =>
{
    image im = new image();
});

假设:窗口image应显示为对话框(模态),并且仅在对PrintFactory.sendTextToLPT1的调用进行时显示。

如果这是正确的,那么像这样的东西可能对你有用:

// Don't forget, you need to dispose modal dialogs
image omg = new image();
// Ensure the dialog has been shown before starting task. That
// way the task knows for sure the dialog's been opened and can
// be closed.
omg.Loaded += (sender, e) =>
{
    // Run the print task in a separate task
    Task.Run(() =>
    {
        PrintFactory.sendTextToLPT1(s);
        // But get back onto the main GUI thread to close the dialog
        Dispatcher.Invoke(() => omg.Close());
    });
};
this.Hide();
omg.ShowDialog();
this.Show();

对于任何打字错误/语法错误等,请提前道歉。希望以上内容足以表达总体观点。

NarzulPeter给出的答案都是正确的。你可以实现任何一个。但是,我知道您的下一个问题将是如何在代码中实现该方法。

可以使用ThreadTask类对象来分离进程。因此,当一个进程正在运行时,其他进程可以在那个时候执行它们的tak。您的登录有两个过程。第一个是将文件发送到打印机,第二个是显示对话框30秒,然后删除文件。您应该创建另一个线程来调用任何一个进程,以便其他进程可以异步执行。

第一步:对打印文件进行单独处理。

Task waitfordelete = Task.Run(() =>
{
    PrintFactory.sendTextToLPT1(s);
});
this.Hide();
omg = new image();
omg.ShowDialog();
this.Show();

第二步:单独处理显示对话框并删除文件。但是,我认为这种方法可能会出错您不能从其他线程更改UI

Task waitfordelete = Task.Run(() =>
{
    Dispatcher.Invoke(() => this.ShowSplashScreen());
});
PrintFactory.sendTextToLPT1(s);

private void ShowSplashScreen()
{
    this.Hide();
    omg = new image(); 
    omg.ShowDialog();
    this.Show();        
}

如果你不想使用线程或任务,那么只需简单地处理Image表单的关闭事件

this.Hide();
omg = new image(); 
omg.Show();
PrintFactory.sendTextToLPT1(s);
omg.FormClosed += (object sender, EventArgs e) => { 
    File.Delete(Path.Combine(Application.StartupPath, Path.GetFileName(img_path.Text));
    this.Show();    
};

Image的形式修改timer_tick事件中的代码,并在删除文件语句后添加this.Close()

void timer1_Elapsed(object sender, ElapsedEventArgs e)
{
    ....
    //File.Delete(Path.Combine(@"''bin'Debug", Filename)); comment this line
    this.Close();
}

我在这里发现了另一个隐藏的问题这里我无法将带有扩展名的旧字符串文件名传递到此表单。任何方法都请帮助我

 void timer1_Elapsed(object sender, ElapsedEventArgs e)
{
    //delete the file using "filePath"
    string Filename = img_path.Text; // here i cannot pass the old string file name with  extension to this form.. Any ways please help me out

为此,您可以在Image类中创建属性,并从父窗体分配文件名。

Image omg = new Image()
omg.FileName = Path.Combine(Application.StartupPath, Path.GetFileName(img_path.Text));
omg.Show();

图像形式的属性将像这个一样创建

public class Image : Form
{
    public string FileName { get; set; }
    public Image()
    {
    }
    void timer1_Elapsed(object sender, ElapsedEventArgs e)
    {
        ....
        File.Delete(Path.Combine(Application.StartupPath, this.Filename));
        this.Close();
    }        
}

注意:使用''bin'debugApplication.StartupPath