我一直在尝试制作一个按钮,它会使用字典从文本文件中删除一行,但是当删除该行时,其余行似乎只是没有值的字典的键,任何帮助? 编辑:在删除行之前,文件如下所示: 信徒,摇滚,665498 原住民,摇滚,597264 删除后文件看起来像如果我删除信徒行: 本机,, :
private void button1_Click(object sender, EventArgs e)
{
string pathsource = @"C:\Users\maher\Downloads\CDs.txt";
string name = textBox1.Text;
StreamReader reader = new StreamReader(pathsource);
Dictionary<string, KeyValuePair<string, string>> MS = new Dictionary<string, KeyValuePair<string, string>>();
KeyValuePair<string, string> kvp = new KeyValuePair<string, string>();
while (reader.Peek() != -1)
{
string s;
string[] fasl;
s = reader.ReadLine();
fasl = s.Split(',');
MS.Add(fasl[0], kvp);
}
reader.Close();
MS.Remove(name);
using (StreamWriter writer = new S treamWriter(pathsource, false))
{
foreach(var entry in MS)
{
writer.WriteLine("{0},{1},{2}", entry.Key, entry.Value.Key,entry.Value.Value);
}
}
MessageBox.Show("CD Removed");
textBox1.Clear();
textBox1.Focus();
}
答案 0 :(得分:0)
您的输出看起来像
的原因本地,,
是你正在为该行编写逗号描述键,kvp.key和kvp.value,但你实际上从未设置过kvp。
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.**WRITE**_EXTERNAL_STORAGE}, PERMISSION_REQUEST_EXTERNAL_STORAGE);