我必须在保存文件之前追加字符串: [英] i hve to append string before save the file:
本文介绍了我必须在保存文件之前追加字符串:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须在保存文件之前附加字符串:
在我的文件夹中,我有很多文件,但是我只从文件夹中过滤掉*.dic
和*.dcm
类型的文件.
但是在保存到硬盘之前,我先附加一些字符串,但是在扩展文件类型*.dic
和*.dcm
之前,我该怎么做:;
i hve to append string before to save the file:
in my folder i hav lot of files but i hve filter out only of type *.dic
and *.dcm
from folder.
but before save to hard disk i hv to append with some string but before extension of file type *.dic
and *.dcm
how can i do that:;
string lookfor = "*.dic;*.dcm";
string[] extension = lookfor.Split(new char[] { ';' });
ArrayList mylist = new ArrayList();
DirectoryInfo di = new DirectoryInfo(txtInputFilePath.Text);
foreach (string ext in extension)
{
mylist.AddRange(di.GetFiles(ext));
foreach (FileInfo fi in mylist)
{
string r = fi.FullName.ToString();
MessageBox.Show(r.ToString());
}
}
推荐答案
更好的解决方案是使用子字符串:
A better solution is to use substring:
foreach (string ext in extension)
{
mylist.AddRange(di.GetFiles(ext));
foreach (FileInfo fi in mylist)
{
string r = fi.FullName.ToString();
int lastDot = r.LastIndexOf('.');
r = r.Substring(0, lastDot) + "hello" + r.Substring(lastDot);
MessageBox.Show(r.ToString());
}
}
尝试通过更改此块..
try by altering this block..
foreach (string ext in extension)
{
mylist.AddRange(di.GetFiles(ext));
foreach (FileInfo fi in mylist)
{
string r = fi.FullName.ToString();
// newly added lines
string[] parts = r.Split('.');
parts[parts.Length-1] +="your text";
r=string.Empty;
int i=0;
for(i=0;i<parts.length-2;i++)>
{
r += parts[i];
}
r += "." + parts[i];
MessageBox.Show(r.ToString());
}
}
希望这会有所帮助.
hope this helps..
这篇关于我必须在保存文件之前追加字符串:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文