Im trying to remove the spaces out of a bunch of file names(pdf's in a directory). I have tried the following. both input and output directories are folderbrowserdialog box's
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(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(" ", "_"));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.