[英]Take the space out of file names and or replace character with something else
Im trying to remove the spaces out of a bunch of file names(pdf's in a directory). 我试图从文件名中删除空格(目录中的pdf)。 I have tried the following.
我尝试了以下方法。 both input and output directories are folderbrowserdialog box's
输入和输出目录都是folderbrowserdialog框的
DirectoryInfo di = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
foreach (var file in di.GetFiles())
{
try
{
File.Copy(file.FullName, outputDir + @"\" + file.Replace(" ", "_"));
}
}
Get the file name out of the file info object: 从文件信息对象中获取文件名:
file.Name.Replace(" ", "_")
Use Path.Combine
to put the path together (more robust across different systems): 使用
Path.Combine
将路径放在一起(跨不同系统更健壮):
Path.Combine(outputDir, file.Name.Replace(" ", "_"))
So: 所以:
di = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
foreach (var file in di.GetFiles()) {
try {
File.Copy(file.FullName, Path.Combine(outputDir, file.Name.Replace(" ", "_")));
}
Try this -- 尝试这个 -
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 { }
}
File.Copy(file.FullName, outputDir + @"\" + file.Name.Replace(" ", "_"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.