如何在使用c#.net复制到任何特定位置之前将数字附加到嵌入文本文件中? [英] How to append a number to embedded text file before copy to any specific location using c#.net?

查看:78
本文介绍了如何在使用c#.net复制到任何特定位置之前将数字附加到嵌入文本文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Windows应用程序。假设我已经嵌入了一个名为file.txt的文本文件,当我点击一个按钮时,它应该将它复制到名称为file1.txt的任何指定位置,再次点击它应该再次复制名称为file2.txt ...等等上。帮助plz。

解决方案

所以当需要保存它时,请阅读它要去的文件夹,并查看名称:

  string  [] paths = Directory.GetFiles( @ < span class =code-string> D:\Temp \, @  file * .txt); 

然后你可以计算出当前最高的数字:

  int  max =  0 ; 
foreach 字符串路径 in 路径)
{
string justTheName = Path.GetFileNameWithoutExtension(path);
匹配m = Regex.Match(justTheName, @ \d +


);
if (m.Success)
{
int i = int .Parse(m.Value);
max = Math.Max(max,i);
}
}
字符串 newFileName = 字符串 .Format(< span class =code-string> @ D:\Temp \ file {0} .txt,max + 1 );


I m using windows application. Suppose i have embedded a text file named file.txt, when i click a button, it should copy it to any specified location with name file1.txt and when click again it should copy it again with name file2.txt...and so on. Help plz.

解决方案

So when it comes to time to save it, read the folder it is going to, and look at tahe names:

string[] paths = Directory.GetFiles(@"D:\Temp\", @"file*.txt");

You can then work out the highest current number from that:

int max = 0;
foreach (string path in paths)
    {
    string justTheName = Path.GetFileNameWithoutExtension(path);
    Match m = Regex.Match(justTheName, @"\d+


"); if (m.Success) { int i = int.Parse(m.Value); max = Math.Max(max, i); } } string newFileName = string.Format(@"D:\Temp\file{0}.txt", max + 1);


这篇关于如何在使用c#.net复制到任何特定位置之前将数字附加到嵌入文本文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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