C# 如何在30秒后删除文件/或在作业完成后删除文件一次
我正在做一个C#项目,我需要在30秒后删除该文件。因此,一旦文件发送到机器,我需要软件计数到30秒,同时显示启动窗体,一旦30秒过了,关闭启动屏幕,然后删除文件 我添加了一个名为“image”的闪屏。所以现在发生的是,只有在启动屏幕关闭后,数据才会发送到打印机。我需要多线程的工作。我的意思是数据应该在一面打印,而启动屏幕应该同时显示。有没有办法让我出来!!。。请帮帮我。 因此,在我的例子中,我将文件复制到bin/debug文件夹。然后将数据发送到机器,同时显示启动屏幕30秒并关闭启动屏幕,然后我需要删除文件 代码:C# 如何在30秒后删除文件/或在作业完成后删除文件一次,c#,wpf,visual-studio-2010,visual-studio,delete-file,C#,Wpf,Visual Studio 2010,Visual Studio,Delete File,我正在做一个C#项目,我需要在30秒后删除该文件。因此,一旦文件发送到机器,我需要软件计数到30秒,同时显示启动窗体,一旦30秒过了,关闭启动屏幕,然后删除文件 我添加了一个名为“image”的闪屏。所以现在发生的是,只有在启动屏幕关闭后,数据才会发送到打印机。我需要多线程的工作。我的意思是数据应该在一面打印,而启动屏幕应该同时显示。有没有办法让我出来!!。。请帮帮我。 因此,在我的例子中,我将文件复制到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));
}
}
像这样的东西
Task waitfordelete = Task.Run(() =>
{
image im = new image();
});
像这样的东西
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();
对于任何打字错误/语法错误等,请提前道歉。希望以上内容足以表达总体想法。假设:窗口<代码>图像代码>应显示为对话框(模式),并且仅在调用<代码>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();
对任何打字错误/语法错误等提前道歉。希望以上内容足以表达总体想法。Narzul和Peter给出的答案都是正确的。你可以实现任何一个。但是,我知道您的下一个问题将是如何在代码中实现该方法
您可以使用线程
或任务
类对象来分隔进程。因此,当一个进程正在运行时,另一个进程可以在那个时候执行其TAK。在您的登录中有两个过程。第一个是将文件发送到打印机,第二个是显示对话框30秒,然后删除文件。您应该创建另一个线程来调用任何一个进程,以便其他进程可以异步执行
1st:对打印文件进行单独处理
Task waitfordelete = Task.Run(() =>
{
PrintFactory.sendTextToLPT1(s);
});
this.Hide();
omg = new image();
omg.ShowDialog();
this.Show();
2nd:对显示对话框进行单独处理并删除文件。但是,我想你可能会在这个方法中得到错误。不能从其他线程更改UI
如果不想使用线程或任务,只需处理
Image
form的关闭事件即可
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\u tick
事件中修改代码,并在delete file语句后添加this.Close()
void timer1_Elapsed(object sender, ElapsedEventArgs e)
{
....
//File.Delete(Path.Combine(@"\\bin\Debug", Filename)); comment this line
this.Close();
}
我在这里发现了另一个隐藏的问题。在这里,我无法将扩展名为的旧字符串文件名传递到此表单。。有什么办法请帮帮我 为此,可以在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();
}
}
注意:使用应用程序。StartupPath
istead of\\bin\debug
Narzul和Peter给出的答案都是正确的。你可以实现任何一个。但是,我知道您的下一个问题将是如何在代码中实现该方法
您可以使用线程
或任务
类对象来分隔进程。因此,当一个进程正在运行时,另一个进程可以在那个时候执行其TAK。在您的登录中有两个过程。第一个是将文件发送到打印机,第二个是显示对话框30秒,然后删除文件。您应该创建另一个线程来调用任何一个进程,以便其他进程可以异步执行
1st:对打印文件进行单独处理
Task waitfordelete = Task.Run(() =>
{
PrintFactory.sendTextToLPT1(s);
});
this.Hide();
omg = new image();
omg.ShowDialog();
this.Show();
2nd:对显示对话框进行单独处理并删除文件。但是,我想你可能会在这个方法中得到错误。不能从其他线程更改UI
如果不想使用线程或任务,只需处理
Image
form的关闭事件即可
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\u tick
事件中修改代码,并在delete file语句后添加this.Close()
void timer1_Elapsed(object sender, ElapsedEventArgs e)
{
....
//File.Delete(Path.Combine(@"\\bin\Debug", Filename)); comment this line
this.Close();
}
我在这里发现了另一个隐藏的问题。在这里,我无法将扩展名为的旧字符串文件名传递到此表单。。有什么办法请帮帮我 为此,可以在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();
}
}
注意:使用
应用程序。StartupPath
istead of\\bin\debug
注意,它看起来很接近复制粘贴您的应用程序。至少要确保为代码的后半部分的作者提供归属,并解释这个问题是如何显著不同的。你谈到将文件发送到机器,然后突然谈到打印机。我很困惑。为什么这个文件要送到机器上?什么时候印刷?启动屏幕的作用是什么?是在您的机器上还是在接收文件的机器上?如果两种解决方案都正确,您可以使用其中任何一种。但是,我知道你在实施上遇到了麻烦。第二件事,您不需要在此处使用ShowDialog()
方法。您可以改用Show()
方法。在这一行之后,您只需要从threading类调用sleep方法,并以毫秒为单位给出间隔。应用程序将在给定的毫秒间隔内停止,然后继续返回。现在,在sleep方法之后,您可以编写代码来删除文件a