如何从一个私人空间获取信息并将其放入另一个空间

发布于 2024-10-15 15:39:56 字数 1122 浏览 1 评论 0原文

我想从一个私有空间中获取信息,然后将其放入另一个私有空间中,我需要这样做,但不能在同一部分中运行它们,因为我被告知这不适用于我想要代码执行的操作。这是无法正常工作的代码,它的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋风の叶未落 2024-10-22 15:39:56

您可以调用backgroundWorker1.RunWorkerAsync(dlg2.SelectedPath)。这会将字符串传递给工作人员。在 DoWork 处理程序中,您可以从 DoWorkEventArgs 实例获取值:

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

You can call backgroundWorker1.RunWorkerAsync(dlg2.SelectedPath). That will pass the string to the worker. In your DoWork handler, you can get the value from the DoWorkEventArgs instance:

string selectedPath = (string)e.Argument;
DirectoryInfo target = new DirectoryInfo(selectedPath);
白云悠悠 2024-10-22 15:39:56

后台工作人员无法访问 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文