2011-02-04 4 views
0

개인 공허에서 정보를 가져 와서 다른 사람에게 넣고 싶습니다.이 작업을 수행해야하며 동일한 섹션에서 실행할 수 없습니다. 그 코드는 내가 원하는 코드로 작동하지 않을 것이다. 여기에 dlg2.selectedPath가 작동하지 않는 코드가 있습니다.이 버튼은 개인 무효 버튼에서 인식됩니다.개인 공백에서 정보를 가져 와서 다른 사람에게 넣는 방법

private void button1_Click(object sender, EventArgs e) 
    { 
     FolderBrowserDialog dlg2 = new FolderBrowserDialog(); 
     if (dlg2.ShowDialog() == DialogResult.OK) 
     //do whatever with dlg.SelectedPath 
     { 
      backgroundWorker1.RunWorkerAsync(); 
     } 
    } 

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
    { 

      DirectoryInfo source = new DirectoryInfo(dlg.SelectedPath); 
      DirectoryInfo target = new DirectoryInfo(dlg2.SelectedPath); 

      DirectoryInfo dir = new DirectoryInfo(dlg.SelectedPath); 
      FileInfo[] fis = dir.GetFiles("*", SearchOption.AllDirectories); 
      foreach (FileInfo fi in fis) 
      { 
       if (fi.LastWriteTime.Date == DateTime.Today.Date) 
       { 
        File.Copy(fi.FullName, target.FullName + "\\" + fi.Name, true); 
       } 
      } 

     } 

어떤 도움을 주실 수 있습니다.

+0

: 당신의 DoWork 핸들러에서는 DoWorkEventArgs 인스턴스에서 값을 얻을 수 있습니다. 관련 : http://stackoverflow.com/questions/4899281/c-backgroundworker-wont-work-with-the-code-i-want-it-to-do –

+0

이들은 코드의 동일한 조각에 대한 두 가지 다른 쿼리입니다 내가 가진 각각의 문제를 해결하기 위해 정보를 얻었습니다. – bobthemac

답변

3

를 참조하십시오. 그것은 문자열을 작업자에게 전달합니다. 이것은 이미 대답했다

string selectedPath = (string)e.Argument; 
DirectoryInfo target = new DirectoryInfo(selectedPath); 
1

백그라운드 작업자가 다른 스레드에서 작동하기 때문에 dlg2.SelectedPath를 처리 할 수 ​​없습니다. UI 스레드에있는 dlg2 인 backgroundWorker는 다른 .net 만든 스레드에 있습니다. Control.Invoke와 Control.InvokeRequired를 사용해야 작동합니다. 당신은 backgroundWorker1.RunWorkerAsync(dlg2.SelectedPath)를 호출 할 수 있습니다

관련 문제