簡體   English   中英

C#WPF內的文本框內的Foreach行

[英]Foreach Line in textbox inside backgroundworker C# WPF

我有多行文本框,我可以使用以下方法獲取每一行的值(以及索引值):

public void doProcess()
{ 
        if (!string.IsNullOrEmpty(Convert.ToString(deliverynumbers.Text)))
        {

            string _deliverynumbers = deliverynumbers.Text;
            string[] delimiter = {Environment.NewLine};
            string[] array_deliverynumbers = _deliverynumbers.Split(delimiter, StringSplitOptions.None);

            int count = array_deliverynumbers.Length;
            int current = 0;

            foreach (string text in array_deliverynumbers)
            {
                    if (!string.IsNullOrEmpty(Convert.ToString(text)))
                    {

                        current++;
                        Double Total_Percentage = count;
                        Double Current_Percentage = current;
                        Double progressAmount = (Current_Percentage / Total_Percentage * 100);
                        //backgroundWorker.ReportProgress(Convert.ToInt32(progressAmount));

                        //could use this to update progress bar
                        //progress.Value = progressAmount;

                        //Do some other processes like for each line, create a file and name it to the value of the line value...


                    }

                }


        }

 }

但是,當我在DoWork調用中嘗試doProgress()時,我得到“類型為'System.InvalidOperationException'的第一次機會異常'...下面是DoWork代碼;

private void DoWork(object sender, DoWorkEventArgs e)
    {
        //This works
        for (int i = 0; i <= 100; i++)
        {
            Thread.Sleep(100);
            backgroundWorker.ReportProgress(i);
        }

        //But this causes the exception
        doProgress();
    }

我什至刪除了doProgress()代碼的各個部分,以查看有效的方法,但沒有一個起作用。

編輯

即使(!string.IsNullOrEmpty(Convert.ToString(deliverynumbers.Text))){}破壞了它。

private void DoWork(object sender, DoWorkEventArgs e)
    {
        if (!string.IsNullOrEmpty(Convert.ToString(deliverynumbers.Text)))
        {

        }
    }

可以正常工作...如下

基本上,這需要2個文本框(一個是路徑值,一個是文件路徑值)和一個多行文本框的值,並且對於多行文本框中的每一行,將文件(文件路徑文本框值)復制到文件夾路徑(路徑文本框)值),然后將復制的文件重命名為該行的值(來自多行文本框)。

簡而言之,此方法是從多行文本框中的列表中批量復制文件並將其重命名為所選文件夾

我的onclick事件

public void begin_process_Click(object sender, RoutedEventArgs e)
    {

        //Hide Form
        form_process.Visibility = Visibility.Hidden;

        //Show Loader (with progress bar)
        loader.Visibility = Visibility.Visible;

        //Get Multiline Text Box Value
        string _deliverynumbers = deliverynumbers.Text;

        //Get source file path
        string sourceFile = file_path.Text;

        //Get copy to path
        string destinationPath = share_drive_folder_path.Text;

        //Create object to pass through backgroundWorker.RunWorkerAsync(args);
        Object[] args = { _deliverynumbers, sourceFile, destinationPath };

        //pass "args" through..
        backgroundWorker.RunWorkerAsync(args);

    }

並且在我的DoWork()方法中-仍然需要異常捕獲以及其他文件夾和文件檢查等

private void DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;

        //Get arguments we passed in onclick event
        Object[] arg = e.Argument as Object[];

        //Do stuff for multiline value - which is via (string)arg[0]
        string[] delimiter = { Environment.NewLine };
        string _deliverynumbers = (string)arg[0];
        string[] array_deliverynumbers = _deliverynumbers.Split(delimiter, StringSplitOptions.None);

        //Generate Indexing
        int count = array_deliverynumbers.Length;
        int current = 0;

        //ForEach Line in multiline TextBox
        foreach (string text in array_deliverynumbers)
        {
            if (!string.IsNullOrEmpty(Convert.ToString(text)))
            {

                //Update Indexing
                current++;

                //Determine Progress bar percentage
                Double Total_Percentage = count;
                Double Current_Percentage = current;
                Double progressAmount = (Current_Percentage / Total_Percentage * 100);

                //Copy source file for each line in TextBox & rename new copied file to the line value & source file format
                string new_filename = text.Substring(2, text.Length - 2);
                string sourceFile = (string)arg[1];
                string destinationPath = (string)arg[2];
                string destinationFileName = new_filename + System.IO.Path.GetExtension(sourceFile);
                string destinationFile = System.IO.Path.Combine(destinationPath, destinationFileName);

                //Check Folder exists
                if (!System.IO.Directory.Exists(destinationPath))
                {
                    System.IO.Directory.CreateDirectory(destinationPath);
                }

                //Copy & Move and rename
                System.IO.File.Copy(sourceFile, destinationFile, true);

                //Sleep for a bit incase processing is to quick
                Thread.Sleep(100);


                //Update progress bar with new amount                    
                backgroundWorker.ReportProgress(Convert.ToInt32(progressAmount));


            }

        }


    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM