asp.net - Copy, Create and Update a file, keeping the original file in C# -
i trying copy file (filename) folder call backup. when file have been copied backup folder, modifysqlfile() read file backup folder , update file in , keep original file (filename) @ usual place. when program run 2nd time, read original file , create copy backup , updated copied file , overwrite previous copied file.
however, not sure codes went wrong , how overwriting of existing file. kindly, me out i'm new 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); }
first answer problem. writing wrong file in modifysqlfile()
method. should destfilename
instead of filename
file.writealllines(filename, filetexts);
secondly, assuming file not huge (<10mb), better , easier way read contents of original file memory, modify contents , write backup. there no need copyfile()