我有一个名为Pro-Okay.txt的文件名,如果它不存在,我正在尝试创建一个名为Pro-okay.txt的新文件。我可以对现有文件进行区分大小写的搜索,但是当代码转到File.WriteAllText("Pro-okay.txt", "")时,它会覆盖已经包含信息的Pro-Okay.txt在其中我不想被覆盖。我可以使用区分大小写的等效项来创建新的 Pro-okay.txt 文件吗?
这是我当前的代码:
public static bool FileExistsCaseSensitive(string filename)
{
string name = Path.GetDirectoryName(filename);
return name != null && Array.Exists(Directory.GetFiles(name), s => s == Path.GetFullPath(filename));
}
public void GetProWords(string word)
{
WordDataSet.Clear();
Words.Clear();
Frequency.Clear();
string[] WordSet;
string fname = Environment.CurrentDirectory + "\\Brain\\Pro-" + word + ".txt";
if (FileExistsCaseSensitive(fname))
{
}
else
{
File.WriteAllText(fname, "");
}
TextReader txtReader = new StreamReader(fname);
try
{
while (txtReader.Peek() != -1)
{
string line = txtReader.ReadLine();
if (line.Contains('~'))
{
WordSet = line.Split('~');
Words.Add(WordSet[0].ToString());
Frequency.Add(Convert.ToInt32(WordSet[1]));
}
}
for (int i = 0; i < Words.Count; i++)
{
WordDataSet.Add(new WordData { Word = Words[i], Frequency = Frequency[i] });
}
}
finally
{
txtReader.Close();
}
}