【发布时间】: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