gpt4 book ai didi

c# - 如果文件已经存在,如何覆盖?

转载 作者:可可西里 更新时间:2023-11-01 08:37:32 27 4
gpt4 key购买 nike

我正在制作音乐播放器。它有两种形式;一个是您播放音乐的主要区域。第二种形式有一个 CheckedListBox,您可以在其中选择所需的 mp3。当我单击一个按钮时,它将选择保存在一个 .txt 文件中,这样我就可以在第一种形式中访问它们,我将在其中将字符串放入音乐播放器查找文件的路径中。

这是我第二种形式的代码,我将所选歌曲保存到 .txt 文件中。

private void selectbtn_Click(object sender, EventArgs e)
{
if (File.Exists(@"C:\Users\Me\Desktop\JAM_MACHINE\JAMS\record.txt"))
{
File.WriteAllText(@"C:\Users\Me\Desktop\JAM_MACHINE\JAMS\record.txt", String.Empty);
}

string[] checkedtitles = new string[checkedListBox1.CheckedItems.Count];

for (int ii = 0; ii < checkedListBox1.CheckedItems.Count; ii++)
{
checkedtitles[ii] = checkedListBox1.CheckedItems[ii].ToString();
}
string selectedSongs = String.Join(Environment.NewLine, checkedtitles);

songRecord.writeRecord(selectedSongs); //I initialised the class containing streamwriter/reader, and called it songRecord
this.Close();
}

问题是,每当我关闭程序并再次打开它时,我都无法重写/清除 .txt 文件。它只是添加到现有文件中。有什么地方我做得不对吗?

这是我的流式阅读器/编写器代码。我很确定我在运行后也关闭了它,但也许有人能找出问题所在:

namespace songss
{
class DataRecord
{
public void writeRecord(string line)
{
StreamWriter sw = null;
try
{
sw = new StreamWriter(@"C:\Users\Me\Desktop\JAM_MACHINE\record.txt", true);
sw.WriteLine(line);
}
catch (FileNotFoundException)
{
Console.WriteLine("Error: File not found.");
}
catch (IOException)
{
Console.WriteLine("Error: IO");
}
catch(Exception)
{
throw;
}
finally
{
if (sw != null)
sw.Close();
}
}

public void readRecord()
{
StreamReader sr = null;
string myInputline;
try
{
sr = new StreamReader(@"C:\Users\Me\Desktop\JAM_MACHINE\record.txt");
while ((myInputline = sr.ReadLine()) != null) ; //readline reads whole line
Console.WriteLine(myInputline);
}
catch (FileNotFoundException)
{
Console.WriteLine("Error: File not found");
}
catch(IOException)
{
Console.WriteLine("Error: IO");
}
catch (Exception)
{
throw;
}
finally
{
if (sr != null)
sr.Close();
}
}
}
}

最佳答案

写入所有文本

File.WriteAllText应该做你想做的事。

Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.

流式写入器

StreamWriter class还有一个覆盖/追加的选项:

Initializes a new instance of the StreamWriter class for the specified file by using the default encoding and buffer size. If the file exists, it can be either overwritten or appended to.

public StreamWriter(
string path,
bool append
)

例子:

using (StreamWriter writer = new StreamWriter("test.txt", false)){ 
writer.Write(textToAdd);
}

查看您的代码,您正在传递 true,这意味着追加。

sw = new StreamWriter(@"C:\Users\Me\Desktop\JAM_MACHINE\record.txt", true);
sw.WriteLine(line);

.NET 精简框架

如果您遇到不支持任何内容(例如紧凑框架)的 .NET 版本,您也可以自己实现 WriteAllText:

static void WriteAllText(string path, string txt) {
var bytes = Encoding.UTF8.GetBytes(txt);
using (var f = File.Open(path, FileMode.Create)) {
f.Write(bytes, 0, bytes.Length);
}
}

关于c# - 如果文件已经存在,如何覆盖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44235689/

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