我们如何在数据网格视图中添加前缀重命名文件名 [英] How can we rename file name in data grid view adding prefix

查看:95
本文介绍了我们如何在数据网格视图中添加前缀重命名文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





需要在数据网格视图中重命名文件,



旧文件名测试。 txt并需要添加前缀作为tes001test.txt,并需要将数字增加到002以用于下一个文件。



我尝试过:



 int CheckBoxRowCounter = 0; 
for(int i = 0; i< dataGridView1.RowCount; i ++){
if(Convert.ToBoolean(dataGridView1.Rows [i] .Cells [0] .Value)){
CheckBoxRowCounter ++;
}
}
if(CheckBoxRowCounter> 0)
{
for(int i = 0; i< dataGridView1.RowCount; i ++)
{

if(Convert.ToBoolean(dataGridView1.Rows [i] .Cells [0] .Value))
{
string SourceFilePath = dataGridView1.Rows [i] .Cells [1] .Value.ToString();
string FileName = new System.IO.FileInfo(SourceFilePath).Name;
var prefix = Regex.Match(SourceFilePath,^ \\D +)。Value;
var number = Regex.Replace(SourceFilePath,^ \\D +,);
var r = int.Parse(number)+ 1;
var newstring = prefix + r.ToString(new string('0',number.Length));
}
}

解决方案

首先,检查你的正则表达式:这将无法正常工作:

 ^ \D + 

匹配文本开头的数字,而您的描述并未显示:tes001text.txt 根本不匹配你的模式。

 ^ tes\D + 

会,但...... Regex.Replace方法(字符串,字符串)(System.Text.RegularExpressions) [ ^ ]不返回它被删除了,所以你试图解析这个号码也会失败,因为你已经特意从你想要解析的字符串中删除了数字!



停止猜测:阅读Regexes,并获取 Expresso [ ^ ] - 它是免费的,它会检查,测试和生成正则表达式。



但是......为什么你甚至试图解析数字时你可能需要上一行中的数字来计算出这个数字?


Hi,

Need to rename file in datagrid view ,

old file name test.txt and need to add prefix as tes001test.txt and need to increment number to 002 for the next file .

What I have tried:

int CheckBoxRowCounter = 0;
            for (int i = 0; i < dataGridView1.RowCount; i++) {
                if (Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value)) {
                    CheckBoxRowCounter++;
                }
            }
            if (CheckBoxRowCounter > 0)
            {
                for (int i = 0; i < dataGridView1.RowCount; i++)
                {

                    if (Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value))
                    {
                        string SourceFilePath = dataGridView1.Rows[i].Cells[1].Value.ToString();
                        string FileName = new System.IO.FileInfo(SourceFilePath).Name;
                        var prefix = Regex.Match(SourceFilePath, "^\\D+").Value;
                        var number = Regex.Replace(SourceFilePath, "^\\D+", "");
                        var r = int.Parse(number) + 1;
                        var newstring = prefix + r.ToString(new string('0', number.Length));
                    }
                }

解决方案

First off, check your regex: that won't work:

^\D+

Matches numbers at the start of the text, and your description doesn't show that: "tes001text.txt" does not match your pattern at all.

^tes\D+

would, but ... Regex.Replace Method (String, String) (System.Text.RegularExpressions)[^] does not return the "bit it removed", so your attempt to parse the number is goign to fail as well because you have specifcally removed the number from the string you are trying to parse!

Stop guessing: read up on Regexes, and get a copy of Expresso[^] - it's free, and it examines, tests, and generates Regular expressions.

But... why the heck are you even trying to parse the number when you presumably need the number from the previous row to work out what number to give this one?


这篇关于我们如何在数据网格视图中添加前缀重命名文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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