c# - C#/.NET 中的图像重命名程序

标签 c# .net visual-studio

我正在开发一个 .NET 程序,该程序旨在迭代选定的目录并允许在显示图像后重命名图像文件。通过我的代码,我感觉程序已经完成了大约 98%,但我使用了 while 循环来等待按下按钮,以便允许重命名图像文件。然而,每次迭代 while 循环时,while 循环都会卡住程序。

如何像 C++ 中那样 system("pause");while 循环暂停而不卡住程序并创建无限循环,或者我怎样才能让 while 循环自动暂停,直到按下按钮?

这里是循环代码的花絮:

paused = true;
bool X = false;
label2.Text = "please choose a file name and press next";
//while statement
while (X == false)
{
    if (paused == false)
    {
        //Renames filename
        string newFileName = filenameTextbox.Text;

        //Adds filename to selected directory where user wishes to send file to
        string outputFile = destinationDirectory + "\\" + intCounter + fileNameOfficial;

        //Pause statement to move to next operand..

        //Copies post iterated file to selected directory
        File.Copy(inputFile, outputFile, true);
        X = true;
    }
}

完整代码如下。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;


namespace recursionBitches
{

    public partial class fileSorter : Form
    {
        bool paused = true;
        public fileSorter()
        {
            InitializeComponent();
        }

        private void browseFrom_Click(object sender, EventArgs e)
        {
            //Warning dialouge for selecting a large file.
            MessageBox.Show("Warning, before selecting the origination file, it is important to note that choosing too large of a directory (for example, my documents or 'C://') more than likely will cause this program to freeze. ");
            MessageBox.Show("You have been warned");
            //Choose the originating dir path. This dir path is to be later sorted using recursion.
            //Once the originating dir path is chosen, it is added to the label above the button.
            if (fromBrowse.ShowDialog(
                ) == DialogResult.OK)
            {
                this.originationLabel.Text = fromBrowse.SelectedPath;
            }
        }


        private void browseTo_Click(object sender, EventArgs e)
        {
            //Choose the send to dir path. This dir path is to later have the files that are sorted sent to it.
            //Once the to dir path is chosen, it is added to the label above the button.
            if (toBrowse.ShowDialog(
               ) == DialogResult.OK)
            {
                this.sendingLabel.Text = toBrowse.SelectedPath;
            }
        }


        private void sortButton_Click(object sender, EventArgs e)
        {
            string fileExtension = "*" + fileExtensionTextbox.Text;
            //Check from path to ensure they are set to a user defined path.
            int intCounter = 1;
            if (originationLabel.Text != "From")
            {
                //Check to path to ensure it is set to a user defined path.
                if (sendingLabel.Text != "To")
                {
                    //Recursion stuff...
                    string sourceDirectory = fromBrowse.SelectedPath;
                    string destinationDirectory = toBrowse.SelectedPath;
                    //Sends origination path to function ae this is a function call. its num = 8675309... I think its name is Jenny.
                    recursiveRecursion(sourceDirectory, destinationDirectory, fileExtension, intCounter);
                }
                else
                {
                    //Message box that says it is required to
                    MessageBox.Show("You dun goofed");
                }
            }
            else
            {
                //Yup, it's a message box.
                //That was an unhelpful comment....
                /Aalmost as unhelpful as this comment
                // This is what happens when I program stoned.
                MessageBox.Show("You dun goofed");
            }

            //Grabs the path of the originating directory.
            //After the originating directory path is choosen, send the path to a recursion function
            //which will search the folders and sort the files in the dir. path.
        }


        private void originationLabel_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Silly user, I am a label. I dont even click");
        }


        private void sendingLabel_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Silly user, I am a label. I dont even click");
        }
        //Recursion function which grabs the directory path, uses a foreach loop to iterate through the files.
        //Whilst each file is iterated through, have the recursive function select the files by extension and copy
        //the files to another directory chosen by the user.
        private void recursiveRecursion(string sourceDirectory, string destinationDirectory, string fileExtension, int intCounter)
        {
            //Select files Path and stuff
            string[] filenames = Directory.GetFiles(sourceDirectory, fileExtension);
            //foreach (string directoryName in directoryNames)
            string[] directoryNames = Directory.GetDirectories(sourceDirectory);
            //dir is the file name, as in for each file name in directory do the thing in this recursion loop thingy.
            foreach (string fileName in filenames)
            {
                try
                {
                    if (File.Exists(fileName))
                    {
                        //Don't use recursion. It is a file
                        //copy files here and stuff.

                        intCounter++;
                        //For the destination file ae dir2 to work, I need to get the file name from the file path.
                        //Currently dir is the file path.
                        //Enter code here to get the file name from the file path.

                        //Displays on label name of the file.
                        this.filesortTextbox.Text = "the file being copied is " + fileName;
                        //Gets filename from path

                        string fileNameOfficial = Path.GetFileName(fileName);
                        //Enters text of filename into filename textbox
                        filenameTextbox.Text = fileNameOfficial;

                        //For copying purposes, adds filename to originating directory path.
                        string inputFile = sourceDirectory + "\\" + fileNameOfficial;

                        //Assigns image from the origination directory.
                        Image img = Image.FromFile(inputFile);
                        // Shows image in the image viewer...
                        iteratedPicturebox.Image = img;
                        iteratedPicturebox.Width = img.Width;
                        iteratedPicturebox.Height = img.Height;

                        //Allows the user to change file name, after changing filename allows
                        //user to go to next file to rename. This is awesome for files...

                        paused = true;
                        bool X = false;
                        label2.Text = "Please choose a file name and press next.";
                        //while statement
                        while (X == false)
                        {
                            if (paused == false)
                            {
                                //Renames filename
                                string newFileName = filenameTextbox.Text;

                                //Adds filename to selected directory where user wishes to send file to.
                                string outputFile = destinationDirectory + "\\" + intCounter + fileNameOfficial;

                                //Pause statement to move to next operand..

                                //Copies post iterated file to selected directory.
                                File.Copy(inputFile, outputFile, true);
                                X = true;
                            }
                        }
                    }
                    else
                    {
                        //This really has an unknown pourpose, admittably keeping here for good luck...
                        Console.WriteLine("{0} is not a valid file or directory.", fileName);
                    }
                }

                catch (Exception e)
                {
                    //What process failed you ask? Good question mate.
                    Console.WriteLine("The process failed: {0}", e.ToString());
                }
            }

            //Long story short for each directory contained in the list of directories.
            //Do the stuff listed in the foreach statement.
            foreach (string directoryName in directoryNames)
            {
                try
                {
                    if (Directory.Exists(directoryName))
                    {
                       //If this is a directory, send the thing through itself.
                       // MessageBox.Show("now going through the "+ directoryName + " directory");
                       recursiveRecursion(directoryName, destinationDirectory, fileExtension, intCounter);
                       this.directoryIterated.Text = "The Current Directory The Files Are being copied from is " + directoryName;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("The process failed: {0}", e.ToString());
                }
            }
        }

        private void fileExtensionTextbox_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {
        }

        private void directoryIterated_Click(object sender, EventArgs e)
        {
            MessageBox.Show("I am a label. I don't even click");
            this.directoryIterated.Text = "You pressed me.";
        }

        private void kill_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void filesortTextbox_Click(object sender, EventArgs e)
        {
            MessageBox.Show("I am a label. I don't even click.");
            this.directoryIterated.Text = "You pressed me.";

        }

        private void iteratedPicturebox_Click(object sender, EventArgs e)
        {
            MessageBox.Show("I am a picture box, I don't even click.");
        }

        private void nextButton_Click(object sender, EventArgs e)
        {
            if (paused == false)
            {
                paused = true;
            }
            else
            {
                paused = false;
            }
        }
    }
}

最佳答案

考虑使用 BackgroundWorker用于您的处理循环。这允许您在后台线程上处理它,并且不会卡住您的主 UI 线程(这就是正在发生的情况)

关于c# - C#/.NET 中的图像重命名程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7762574/

相关文章:

c# - 将依赖项注入(inject)动态加载的 .dll (.net core)

c# - Windows Mobile native 异常 C#/C++

c# - .net 中的 AutoClass 是什么?

C# MVVM 升级到 EF5 - 实体成员函数去哪儿了?

c# - 我是否必须在单元测试中伪造一个值对象

c# - NAudio 演示不再有效

.net - 将参数与 EntityFramework 和 `FromSql` 一起使用

c# - 从 SSL + WatiN 下载网页图像

c# - 从其他类/范围获取 var

c++ - 如何使 Visual Studio (C++) 显示 "included by"-链?