我试图从一堆文件名中删除空格(目录中的pdf)。我尝试了以下内容。输入和输出目录都是folderbrowserdialog box的
DirectoryInfo di = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
foreach (var file in di.GetFiles())
{
try
{
File.Copy(file.FullName, outputDir + @"\" + file.Replace(" ", "_"));
}
}
答案 0 :(得分:7)
从文件信息对象中获取文件名:
file.Name.Replace(" ", "_")
使用Path.Combine
将路径放在一起(在不同系统中更加健壮):
Path.Combine(outputDir, file.Name.Replace(" ", "_"))
所以:
di = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
foreach (var file in di.GetFiles()) {
try {
File.Copy(file.FullName, Path.Combine(outputDir, file.Name.Replace(" ", "_")));
}
答案 1 :(得分:0)
试试这个 -
DirectoryInfo di = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
foreach (var file in di.GetFiles())
{
try
{
File.Copy(file.FullName, Path.Combine(outputDir, Path.GetFileName(file.FullName).Replace(" ", "_")));
}
catch { }
}
答案 2 :(得分:0)
File.Copy(file.FullName, outputDir + @"\" + file.Name.Replace(" ", "_"));