【问题标题】:How to delete the file after 30 seconds/ or delete the file once after the job is completed如何在 30 秒后删除文件/或在作业完成后删除文件一次
【发布时间】:2014-12-24 14:49:24
【问题描述】:

我正在处理一个 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));
    }

}

【问题讨论】:

  • 请注意,它看起来接近于复制粘贴您的previous question。至少确保提供代码后半部分的作者的归属,并解释这个问题有何显着不同。
  • 您谈到将文件发送到机器上,然后突然又谈到打印机。我糊涂了。为什么这个文件会进入机器?什么时候打印?闪屏是干什么用的?是在你的机器上还是在接收文件的机器上?
  • 鉴于两种解决方案都是正确的,您可以使用其中任何一种。但是,我知道你在实施它时遇到了麻烦。第二件事你不需要在这里使用ShowDialog() 方法。您可以改用Show() 方法。在这一行之后,您只需要从线程类调用 sleep 方法并以毫秒为单位给出间隔。应用程序将以毫秒为单位停止给定的时间间隔,然后它将继续返回。现在,在 sleep 方法之后,您可以编写代码来删除文件,然后您可以通过调用 omg.Close() 方法关闭启动窗口。

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


【解决方案1】:

类似的东西????

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

【讨论】:

    【解决方案2】:

    假设:窗口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();
    

    对于任何拼写错误/语法错误/等,请提前致歉。希望以上内容足以表达总体思路。

    【讨论】:

    • 感谢您的回答,但我现在不知道如何实现它...它向我展示了许多错误...:P
    • 我知道我的原始编辑中存在一些错字,但截至昨天它们已被修复。您正在使用最新版本的答案吗?如果是这样,您需要比“许多错误”更具体。
    【解决方案3】:

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

    您可以使用ThreadTask 类对象来分隔进程。因此,当一个进程正在运行时,其他进程可以在那个时候执行它们的任务。您的登录中有两个过程。第一个是将文件发送到打印机,第二个是显示对话框 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();
    

    Image形式的属性会这样创建

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

    注意:使用Application.StartupPath 代替\\bin\debug

    【讨论】:

    • 非常感谢.. 但我在所有行中都遇到错误:-( 在ask waitfordelete = Task.Run(() => 行中,它问我你是否想为任务创建一个类!
    • 在这一行中它询问(object sender, EventArgs e) => (File.Delete(Path.Combine(Application.StartupPath, Path.GetFileName(img_path.Text)))); 不符合委托参数类型
    猜你喜欢
    • 2014-12-24
    • 1970-01-01
    • 1970-01-01
    • 2011-02-22
    • 2019-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-27
    相关资源
    最近更新 更多