[英]Changing a text in a Text file with C#
in my program, user names and passwords are coming from text files.在我的程序中,用户名和密码来自文本文件。 I want to update the password by pressing a button when necessary, what can I do for this?
我想在必要时按一个按钮来更新密码,我该怎么办?
private void Form1_Load(object sender, EventArgs e)
{
this.IsMdiContainer = true;
string[] firmalar = System.IO.File.ReadAllLines(@"Data\\sirket.txt", System.Text.Encoding.GetEncoding("UTF-8"));
foreach (string str in firmalar)
{
SirketList.Items.Add(str);
}
string[] kullanici = System.IO.File.ReadAllLines(@"Data\\kullanici.txt", System.Text.Encoding.GetEncoding("UTF-8"));
foreach (string str in kullanici)
{
KullaniciList.Items.Add(str);
}
string[] kod = System.IO.File.ReadAllLines(@"Data\\kod.txt", System.Text.Encoding.GetEncoding("UTF-8"));
foreach (string str in kod)
{
kodList.Items.Add(str);
}
string[] sistem = System.IO.File.ReadAllLines(@"Data\\sistem.txt", System.Text.Encoding.GetEncoding("UTF-8"));
foreach (string str in sistem)
{
sistemList.Items.Add(str);
}
string[] isveren = System.IO.File.ReadAllLines(@"Data\\isveren.txt", System.Text.Encoding.GetEncoding("UTF-8"));
foreach (string str in isveren)
{
isverenList.Items.Add(str);
}
string[] araci = System.IO.File.ReadAllLines(@"Data\\araci.txt", System.Text.Encoding.GetEncoding("UTF-8"));
foreach (string str in araci)
{
ListAraci.Items.Add(str);
}
}
File static class is your friend here. 文件static class 是您的朋友。
Seems you use different files for each one of the properties you want to load/save, so you should bind to your save button code that looks like this:似乎您为要加载/保存的每个属性使用不同的文件,因此您应该绑定到如下所示的保存按钮代码:
public static async Task SavePasswordAsync()
{
await File.WriteAllTextAsync(kodList, text);
}
If I were you, I would store all this information in a single file.如果我是你,我会将所有这些信息存储在一个文件中。 And the data would be located line by line.
数据将逐行定位。 Then I would read it by dividing it by lines as I read, change the new value and rewrite it
然后我会在阅读时将其除以行,更改新值并重写它
Thank you all.谢谢你们。 This is how I solved it.
这就是我解决它的方法。
string text = File.ReadAllText("test.txt");
text = text.Replace("some text", "new value");
File.WriteAllText("test.txt", text);
In my code it is like this在我的代码中是这样的
String strFile = File.ReadAllText(@"Data\\sistem.txt");
strFile = strFile.Replace(SifreDegistirSistem.Text, newPass.Text);
File.WriteAllText(@"Data\\sistem.txt", strFile);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.