2014-10-28 3 views
-2

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; 
    } 
+0

Thread.Sleep (30000) ?? – Fratyx

+1

어떤 부분이 당신을 트립하고 있습니다. 30 초를 기다리고 있니? [타이머 클래스] (http://msdn.microsoft.com/en-us/library/system.windows.forms.timer (v = vs.110) .aspx)를 살펴보십시오. –

+1

시작 화면이 있습니까? – Shaharyar

답변

1

SplashScreen로 사용할 수있는 새 양식을 만듭니다. 되는 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

내 기본 양식 및 하위 양식에서 이것을 사용하는 방법 –

+0

@StacyKebler Ooopss! 나는 주요 부분을 잊어 버렸다. 지금 추가하기. – Shaharyar

관련 문제