如何在C#windows应用程序中的列表框中重命名多个图像 [英] How to rename multiple images in a List-Box in C# windows application

查看:44
本文介绍了如何在C#windows应用程序中的列表框中重命名多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手。我正在构建一个窗口表单,可以重命名列表框中的多个文件。 
$


文件实际上是图像(.png),我想要做的是只需将图像从"image1.png,image2.png,image3.png",...等重命名为"newname1.pnage,newname2.png,newname3.png",然后再按b $ b

我成功地使它可以重命名单个图像,但是我认为必须有一个for循环来进行多个图像重命名(我可能错了)。那么你能帮我解决这个问题吗?。$


这里是重命名按钮的代码....谢谢。



private void buttonRename_Click(对象发件人,EventArgs e)

        {

            if(outputListBox.SelectedIndex> = 0)

            {

$
                string fileToRename = outputListBox.Items [outputListBox.SelectedIndex] .ToString();

                  string newFileName = newNametextBox.Text;
$


                if(!string.IsNullOrEmpty(newFileName))

                {

                    string fileName = Path.GetFileName(fileToRename);

                    string newFilePath = fileToRename.Replace(fileName,newFileName);

                    System.IO.File.Move(fileToRename,newFilePath);

                    outputListBox.Items [outputListBox.SelectedIndex] = newFilePath;

                }¥b $ b            }¥b $ b           否则

            {

                MessageBox.Show("上面列表中没有要选择的文件","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);

            }¥b $ b       你可以这样做:

 var newFileName =" newName" ;; 
var fileToRename ="" ;;
var idx = 1;

foreach(var listItem in outputListBox.SelectedItems)
{
fileToRename = selectedItem.ToString();

if(!string.IsNullOrEmpty(newFileName))
{
newFileName + = idx.ToString();
string fileName = Path.GetFileName(fileToRename);
string newFilePath = fileToRename.Replace(fileName,newFileName);
System.IO.File.Move(fileToRename,newFilePath);
}
}

但是,您无法在此foreach循环中更改所选项目。您需要重新加载列表或收集新的文件名和索引,并创建另一个将更改列表框元素的循环。


I am new to C#. I am building a windows form that can rename multiple files in a List Box. 

Files are actually images (.png) and what i am trying to do is just rename the images from "image1.png, image2.png, image3.png", ... etc , to "newname1.pnage, newname2.png, newname3.png"

I was successful to make it work for renaming single image, however and I think there must me a for loop there to do the multiple image rename (I might be wrong). So would you kindly helping me resolving this issue?.

here is my code of the rename button.... Thank you.

private void buttonRename_Click(object sender, EventArgs e)
        {
            if (outputListBox.SelectedIndex >= 0)
            {

                string fileToRename = outputListBox.Items[outputListBox.SelectedIndex].ToString();
                 string newFileName = newNametextBox.Text;

                if (!string.IsNullOrEmpty(newFileName))
                {
                    string fileName = Path.GetFileName(fileToRename);
                    string newFilePath = fileToRename.Replace(fileName, newFileName);
                    System.IO.File.Move(fileToRename, newFilePath);
                    outputListBox.Items[outputListBox.SelectedIndex] = newFilePath;
                }
            }
            else
            {
                MessageBox.Show("There is no Files in the Above list to be Selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

解决方案

You can do something like this:

var newFileName = "newName";
var fileToRename = "";
var idx = 1;

foreach(var selectedItem in outputListBox.SelectedItems)
{
    fileToRename = selectedItem.ToString();

    if (!string.IsNullOrEmpty(newFileName))
    {
        newFileName += idx.ToString(); 
        string fileName = Path.GetFileName(fileToRename);
        string newFilePath = fileToRename.Replace(fileName, newFileName);
        System.IO.File.Move(fileToRename, newFilePath);
    }
}

However, you can not change the selected item in this foreach loop. You need to reload the list or collect new file names and indexes and create another loop that will change the elements of the listbox.


这篇关于如何在C#windows应用程序中的列表框中重命名多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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