我如何从一个私人空白中获取信息,然后将其放入另一个空白中 [英] how do I take information from one private void and put it in another

查看:96
本文介绍了我如何从一个私人空白中获取信息,然后将其放入另一个空白中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个私有无效空间中获取信息,然后将其放入另一个私有空间中,我需要这样做,并且无法在同一部分中运行它们,因为有人告诉我,这种方法不能与我想要的代码一起工作.这是无法正常运行的代码dlg2.selectedPath,它需要从需要的按钮private void中进行识别.

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.

推荐答案

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

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);

这篇关于我如何从一个私人空白中获取信息,然后将其放入另一个空白中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆