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

时间:2011-10-14 02:50:57

标签: 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;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:3)

考虑使用BackgroundWorker进行处理循环。这允许您在后台线程上处理它,并且不会冻结您的主UI线程(正在发生的事情)

答案 1 :(得分:2)

我会显示一个对话框:

,而不是试图停止执行
  1. 创建表单,例如:FileRenameDialog
  2. 将公共属性添加到表单,您可以使用它来设置From:text等。
  3. 创建表单实例。例如:FileRenameDialog reuseme
  4. 每当用户需要进行选择时,请填充对话框中的文本。例如:reuseme.Source = "The current file being copied...";
  5. 以用户身份向用户显示对话框,例如:var dlgResult = reuseme.ShowDialog(this);
  6. 根据结果执行您需要的操作,例如:if (dlgResult == DialogResult.OK) {...do stuff with the properties of reuseme...}