gpt4 book ai didi

c# - 为什么我的File.Move()无法正常工作?

转载 作者:太空宇宙 更新时间:2023-11-03 17:59:54 25 4
gpt4 key购买 nike

我不确定在这里我到底在做什么错...但是我注意到我的File.Move()没有重命名任何文件。
另外,有人知道我在第二个循环中如何用路径列表和已清理文件名填充.txt文件吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{

//recurse through files. Let user press 'ok' to move onto next step
string[] files = Directory.GetFiles(@"C:\Documents and Settings\jane.doe\Desktop\~Test Folder for [SharePoint] %testing", "*.*", SearchOption.AllDirectories);
foreach (string file in files)
{
Console.Write(file + "\r\n");
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey(true);
//End section

//Regex -- find invalid chars
string pattern = " *[\\~#%&*{}/<>?|\"-]+ *";
string replacement = " ";
Regex regEx = new Regex(pattern);

string[] fileDrive = Directory.GetFiles(@"C:\Documents and Settings\jane.doe\Desktop\~Test Folder for [SharePoint] %testing", "*.*", SearchOption.AllDirectories);
List<string> filePath = new List<string>();

//clean out file -- remove the path name so file name only shows
string result;
foreach(string fileNames in fileDrive)
{
result = Path.GetFileName(fileNames);
filePath.Add(result);

}

StreamWriter sw = new StreamWriter(@"C:\Documents and Settings\jane.doe\Desktop\~Test Folder for [SharePoint] %testing\File_Renames.txt");

//Sanitize and remove invalid chars
foreach(string Files2 in filePath)
{
try
{
string sanitized = regEx.Replace(Files2, replacement);
sw.Write(sanitized + "\r\n");
System.IO.File.Move(Files2, sanitized);
System.IO.File.Delete(Files2);




}
catch (Exception ex)
{
Console.Write(ex);
}


}
sw.Close();

}

}


}

我是C#的新手,尝试编写一个通过特定驱动器递归的应用程序,查找无效字符(按RegEx模式指定),将其从文件名中删除,然后编写一个具有路径名和更正的文件名。

有任何想法吗?

最佳答案

您的文件路径列表仅包含文件名。您已在对Path.GetFileName()的调用中从其中删除了目录信息,因此File.Move在应用程序的默认目录(而不是其原始位置)中查找目标文件。

我认为您用于保存经过清理的文件名的代码是正确的。但是,您应该在StreamWriter周围使用using()构造,如下所述,以确保完成后关闭文件。

//clean out file -- remove the path name so file name only shows
string result;
foreach(string fileNames in fileDrive)
{
// result = Path.GetFileName(fileNames); // don't do this.
filePath.Add(fileNames);
}

using (StreamWriter sw = new StreamWriter(@"C:\Documents and Settings\jane.doe\Desktop\~Test Folder for [SharePoint] %testing\File_Renames.txt"))
{
//Sanitize and remove invalid chars
foreach(string Files2 in filePath)
{
try
{
string filenameOnly = Path.GetFileName(Files2);
string pathOnly = Path.GetDirectoryName(Files2);
string sanitizedFilename = regEx.Replace(filenameOnly, replacement);
string sanitized = Path.Combine(pathOnly, sanitizedFilename);
sw.Write(sanitized + "\r\n");
System.IO.File.Move(Files2, sanitized);
}
catch
{
}
}
}

关于c# - 为什么我的File.Move()无法正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3054513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com