gpt4 book ai didi

c# - 在另一个目录中创建唯一文件

转载 作者:行者123 更新时间:2023-11-30 17:33:30 27 4
gpt4 key购买 nike

目前我正在从我指定的目录 (sourceDirectory) 中获取多个 .txt 文件。我正在生成与 .txt 文件同名的新 .csv 文件 - 每个 .txt 文件对应一个 .csv 文件。

但是我想在我指定的另一个目录 (directoryPath) 中生成这些新文件。如果我运行我的程序,它会在初始目录中创建这些文件,但是如果我再次运行我的程序,它现在会在目标目录中生成文件。

以下是我完成上述内容的代码:

static void Main(string[] args)
{
string sourceDirectory = @"C:directoryWhereTXTFilesAre";

var txtFiles = Directory.EnumerateFiles(sourceDirectory, "*.txt", SearchOption.AllDirectories);

foreach (string currentFile in txtFiles)
{
readFile(currentFile);
}

string directoryPath = @"C:\destinationForCSVFiles";
}

然后我根据原始 .txt 文件创建新的 .csv 文件名,如下所示:

static FileStream CreateFileWithUniqueName(string folder, string fileName, int maxAttempts = 1024)
{
var fileBase = Path.GetFileNameWithoutExtension(fileName);
var ext = Path.GetExtension(fileName);

// build hash set of filenames for performance
var files = new HashSet<string> (Directory.GetFiles(folder));

for (var index = 0; index < maxAttempts; index++)
{
// first try with the original filename, else try incrementally adding an index
var name = (index == 0)
? fileName
: String.Format("{0} ({1}){2}", fileBase, index, ext);

// check if exists
var fullPath = Path.Combine(folder, name);
string CSVfileName = Path.ChangeExtension(fullPath, ".csv");
if (files.Contains(CSVfileName))
continue;

// try to create the file
try
{
return new FileStream(CSVfileName, FileMode.CreateNew, FileAccess.Write);
}
catch (DirectoryNotFoundException) { throw; }
catch (DriveNotFoundException) { throw; }
catch (IOException)
{

}
}

我不明白为什么它最初在 .txt 文件所在的同一目录中创建 .csv 文件,然后在我第二次运行我的代码时它在 directoryPath 中创建它们。

期望的输出:sourceDirectory 保持原样,只有 .txt 文件和 directoryPath 来保存 .csv 文件。

我调用 CreateFileWithUniqueName 的唯一其他地方是在我的 readFile 方法中,代码如下:

     using (var stream = CreateFileWithUniqueName(@"C:destinationFilePath", currentFile))
{
Console.WriteLine("Created \"" + stream.Name + "\"");
newFileName = stream.Name;
Globals.CleanedFileName = newFileName;
}

最佳答案

看来您传递的是源文件的完整文件名。这会混淆 CreateFileWithUniqueFilename 中的 Path.Combine,因为您陷入了在 Path.Combine 的文档中发现的这个微妙的评论中。

paths should be an array of the parts of the path to combine. If the one of the subsequent paths is an absolute path, then the combine operation resets starting with that absolute path, discarding all previous combined paths.

你可以很容易地修复它

 using (var stream = CreateFileWithUniqueName(@"C:\destinationFilePath",  
Path.GetFileName(currentFile)))
{
Console.WriteLine("Created \"" + stream.Name + "\"");
newFileName = stream.Name;
Globals.CleanedFileName = newFileName;
}

或者更好地在 CreateFileWithUniqueName 中提取没有路径的文件名

static FileStream CreateFileWithUniqueName(string folder, string fileName, int maxAttempts = 1024)
{
var fileBase = Path.GetFileName(fileName);
fileBase = Path.GetFileNameWithoutExtension(fileBase);
var ext = Path.GetExtension(fileBase);

此外,您应该使用清理后的文件名构建您的 CSVfileName

    var name = (index == 0)
? String.Format("{0}{1}", fileBase, ext);
: String.Format("{0} ({1}){2}", fileBase, index, ext);
var fullPath = Path.Combine(folder, name);
string CSVfileName = Path.ChangeExtension(fullPath, ".csv");
if (files.Contains(CSVfileName))
continue;

关于c# - 在另一个目录中创建唯一文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43782841/

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