I am trying to copy my file (fileName) into a folder call backup. When the file have been successfully copied into the backup folder, the modifySQLFile() will than read the file from backup folder and update the file in it and keep the original file (fileName) at it usual place. When the program run for the 2nd time, it will read the original file and create another copy to backup and updated the copied file and overwrite the previous copied file.
However, i am not sure what my codes went wrong and how to do the overwriting of the existing file. Kindly, help me out as i'm new to this.
public string fileName = "DEPOT-Pub_Sub_Combined (wo CardHolder).sql";
private void modifySQLFile()
{
CopyFile();
string[] fileTexts = File.ReadAllLines(@"backup\DEPOT-Pub_Sub_Combined (wo CardHolder).sql");
int counter = 0;
//File processing
foreach (string line in fileTexts)
{
//only process non-comments line
if (line.StartsWith("--") == false)
{
//replace instances of server name
if (line.Contains(SERVERNAME) == true)
{
fileTexts[counter] = fileTexts[counter].Replace(SERVERNAME, textBox1.Text);
}
if (line.Contains(ACCESSID) == true)
{
fileTexts[counter] = fileTexts[counter].Replace(ACCESSID, textBox2.Text);
}
if(line.Contains(NETWORKID) == true)
{
fileTexts[counter] = fileTexts[counter].Replace(NETWORKID, textBox2.Text);
}
}
counter++;
}
//update file
File.WriteAllLines(fileName, fileTexts);
MessageBox.Show("Completed!");
}
private void CopyFile()
{
string targetPath = @"backup";
string destFile = Path.Combine(targetPath, fileName);
if(!Directory.Exists(targetPath))
{
Directory.CreateDirectory(targetPath);
}
File.Copy(fileName, destFile, true);
}