如何从一个私人空间获取信息并将其放入另一个空间
我想从一个私有空间中获取信息,然后将其放入另一个私有空间中,我需要这样做,但不能在同一部分中运行它们,因为我被告知这不适用于我想要代码执行的操作。这是无法正常工作的代码,它的 dlg2.selectedPath 可以从需要的按钮 private void 中识别出来。
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);
}
}
}
任何帮助将不胜感激。
i want to take information from one private void and then put it into another I need to do this and cannot run them in the same section because I have been told that wont work with what i want the code to do. here is the code that isn't working its the dlg2.selectedPath that inst being recognised from the button private void where it needs to be.
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);
}
}
}
any help will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以调用
backgroundWorker1.RunWorkerAsync(dlg2.SelectedPath)
。这会将字符串传递给工作人员。在DoWork
处理程序中,您可以从DoWorkEventArgs
实例获取值:You can call
backgroundWorker1.RunWorkerAsync(dlg2.SelectedPath)
. That will pass the string to the worker. In yourDoWork
handler, you can get the value from theDoWorkEventArgs
instance:后台工作人员无法访问 dlg2.SelectedPath 因为它在另一个线程中工作。 dlg2 它位于 UI 线程中,backgroundWorker 位于另一个 .net 创建的线程中。您必须使用 Control.Invoke 和 Control.InvokeRequired 才能使其工作。
参见
控制调用
The background worker can't acess the dlg2.SelectedPath bacause it works in another thread. the dlg2 it's in the UI thread, the backgroundWorker, is in an other .net created thread. You must use Control.Invoke, and the Control.InvokeRequired to make it work.
SEE
Control Invoke