如何重命名位置中的两个文件 [英] How Do I Rename Two Files In A Location
问题描述
我有两个文件LS_PRSAHD_61417920_Cleaned.txt和LS_PRSAHD_67543245_Cleaned.txt。我想将文件重命名为LS_PRSAHD_61417920.txt和LS_PRSAHD_67543245.txt。
我试过这个。但不工作。
I have two files LS_PRSAHD_61417920_Cleaned.txt and LS_PRSAHD_67543245_Cleaned.txt. I want to rename the files as LS_PRSAHD_61417920.txt and LS_PRSAHD_67543245.txt.
I have tried this one . but not working.
for (int i = 0; i < FilesHD.Length; i++)
{
FileInfo fi = new FileInfo(FilesHD[i]);
sourcefilename = fi.Name;
string cleanedfiles = Sourcefolder + sourcefilename;
string originalfiles = sourcefilename.Substring(0, sourcefilename.Length - 12) + ".txt";
if (File.Exists(cleanedfiles))
{
File.Move(cleanedfiles, originalfiles);
}
}
请提供一些信息。
Kindly give some info.
推荐答案
假设你没有排除任何例外
assuming that you dont have any exception in line
sourcefilename = fi.Name;
你的档案存在check在这里返回false(可能):
your file existence check returns false (possibly) here:
if (File.Exists(cleanedfiles))
,因为文件路径创建不正确
because of improper file path creation in line
string cleanedfiles = Sourcefolder + sourcefilename;
使用 Path.Combine
获取方法一个正确的完整路径。
use Path.Combine
method to get a proper full path.
cleanedfiles= Path.Combine(Sourcefolder, sourcefilename);
并且可以肯定的是,你可以为if(File.Exists)添加'else'部分
and to be sure, you can add 'else' part for the if(File.Exists)
这篇关于如何重命名位置中的两个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!