如何在30秒后删除文件?
问题描述:
我正在处理一个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
{
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;
}
答
创建一个新的形式,用它作为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)。
+4
如何在我的主表格和子表格中使用这个 – 2014-10-29 00:30:46
+0
@StacyKebler Ooopss!我忘了主要部分。现在添加它 – Shaharyar 2014-10-29 06:32:23
Thread.Sleep(30000)?? – Fratyx 2014-10-28 21:14:03
哪一部分让你失望。等待30秒?看看[Timer类](http://msdn.microsoft.com/en-us/library/system.windows.forms.timer(v = vs.110).aspx)。 – 2014-10-28 21:14:47
你有没有启动画面? – Shaharyar 2014-10-28 21:15:10