我正在使用一些if ... else条件来检查C#中Textbox中的一些更改。这是代码。
private void button1_Click(object sender, EventArgs e)
{
if (txtUsername.Text != UsernameS)
{
string json = File.ReadAllText("settings.json");
dynamic jsonObj = JsonConvert.DeserializeObject(json);
jsonObj["Bots"][0]["Username"] = txtUsername.Text;
string output = JsonConvert.SerializeObject(jsonObj, Formatting.Indented);
File.WriteAllText("settings.json", output);
}
if (txtPassword.Text != PasswordS)
{
string json = File.ReadAllText("settings.json");
dynamic jsonObj = JsonConvert.DeserializeObject(json);
jsonObj["Bots"][0]["Password"] = txtPassword.Text;
string output = JsonConvert.SerializeObject(jsonObj, Formatting.Indented);
File.WriteAllText("settings.json", output);
}
var filename = System.Reflection.Assembly.GetExecutingAssembly().Location;
System.Diagnostics.Process.Start(filename);
// Closes the current process
Environment.Exit(0);
}
好的,现在我想做的是,如果没有变化,我不希望它重启。虽然我也希望同时修复所有更改。也就是说,通过所有if条件然后重新启动。使用我当前的代码,即使没有任何更改,应用程序也会重新启动。请帮助:)
答案 0 :(得分:1)
您可以使用||检查其中任何一个是否有某些变化运营商。如果这些条件中的任何一个返回true,则代码将执行。
private void button1_Click(object sender, EventArgs e)
{
if (txtUsername.Text != UsernameS)
{
string json = File.ReadAllText("settings.json");
dynamic jsonObj = JsonConvert.DeserializeObject(json);
jsonObj["Bots"][0]["Username"] = txtUsername.Text;
string output = JsonConvert.SerializeObject(jsonObj, Formatting.Indented);
File.WriteAllText("settings.json", output);
}
if (txtPassword.Text != PasswordS)
{
string json = File.ReadAllText("settings.json");
dynamic jsonObj = JsonConvert.DeserializeObject(json);
jsonObj["Bots"][0]["Password"] = txtPassword.Text;
string output = JsonConvert.SerializeObject(jsonObj, Formatting.Indented);
File.WriteAllText("settings.json", output);
}
if(txtUsername.Text != UsernameS || txtPassword.Text != PasswordS)
{
var filename = System.Reflection.Assembly.GetExecutingAssembly().Location;
System.Diagnostics.Process.Start(filename);
// Closes the current process
Environment.Exit(0);
}
}