this is entire code, i want to move file (in button btnSave) and if file exist to overwrite that file, how to do that?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace ToEditTxt
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
btnEdit.Enabled = false;
btnEdit.BackColor = Color.White;
btnSave.Enabled = false;
btnSave.BackColor = Color.White;
}
private void btnLoad_Click(object sender, EventArgs e)
{
btnLoad.Enabled = false;
btnLoad.BackColor = Color.White;
btnEdit.Enabled = true;
btnEdit.BackColor = System.Drawing.ColorTranslator.FromHtml("#d3d3d3");
openFileDialog1.ShowDialog();
label1.Text = openFileDialog1.FileName;
label2.Text = System.IO.Path.GetFileName(openFileDialog1.FileName);
label3.Text = System.IO.Path.GetFileNameWithoutExtension(openFileDialog1.FileName);
label4.Visible = true;
}
private void btnEdit_Click(object sender, EventArgs e)
{
btnEdit.Enabled = false;
btnEdit.BackColor = Color.White;
string textToAppend = "1";
List<string> lines = new List<string>();
foreach (var line in File.ReadAllLines(label1.Text))
{
lines.Add(line + textToAppend);
}
File.WriteAllLines(label1.Text, lines.ToArray());
btnSave.Enabled = true;
btnSave.BackColor = System.Drawing.ColorTranslator.FromHtml("#d3d3d3");
}
private void btnSave_Click(object sender, EventArgs e)
{
try
{
System.IO.FileInfo f = new System.IO.FileInfo(label1.Text);
f.MoveTo(@"c:\q\" + label3.Text + ".rpt");
if (File.Exists(@"c:\q\"))
{
File.Delete(@"c:\q\");
File.Move(label3.Text + ".rpt", @"c:\q\");
}
File.Move(label3.Text + ".rpt", @"c:\q\");
}
catch (IOException)
{
label1.ResetText();
label2.ResetText();
label3.ResetText();
this.Refresh();
this.ResetText();
this.Update();
btnLoad.Enabled = true;
btnEdit.Enabled = true;
btnLoad.BackColor = System.Drawing.ColorTranslator.FromHtml("#d3d3d3");
btnEdit.BackColor = System.Drawing.ColorTranslator.FromHtml("#d3d3d3");
label5.Visible = true;
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
</string></string>