Replacing File if Exist in a Directory using C# -
I want to upload a file in C # GUI and check whether it exists in that particular folder. But why is it that the first file already exists, even if another file is changed? I have two upload buttons.
Two buttons.
Private Zero Upload_1_ Click (Object Sender, EventArgs e) {OpenFileDialog file = new OpenFileDialog (); File.Title = "Browse the file"; File.Filter = "PDF files (* .pdf) | * .pdf |" + "Images (* .ppm; * .jpg; * .jpg; * .gif; * .png * * .tif) * * .mpp; *. Jpeg; * .jpg; * .gif; * .png; * .TIFF | "+" All Files (*. *) | *. * "; If (file.ShowDialog () == DialogResult.OK) {string file_path = path.gate directoryname (file.FileName.ToString ()); String file_obj = path. GetFileName (file.FileName.ToString ()); String file_itself = file_path + "\\" + file_obj; Upload_label1.Text = file_itself; }} Private Zero Upload_2_ Click (Object Sender, EventArgs e) {OpenFileDialog file1 = New OpenFileDialog (); File1.Title = "Browse File"; File1.Filter = "PDF files (* .pdf) | * .pdf |" + "Images (* .ppm; * .jpg; * .jpg; * .gif; * .png * * .tif) * * .mpp; *. Jpeg; * .jpg; * .gif; * .png; * .TIFF | "+" All Files (*. *) | *. * "; If (file1.ShowDialog () == DialogResult.OK) {string file_path1 = path.gate directoryname (file1.FileName.ToString ()); String file_obj1 = Path.GetFileName (file1.FileName.ToString ()); String file_itself1 = file_path1 + "\\" + file_obj1; Upload_label2.Text = file_itself1; }} Update button
Private Zero update_Click (Object Sender, EventArgs e) {try {string ext = upload_label1.Text; Ext = ext.Substring (ext.Length - 3, 3); If (file.exist (@ "uploads \\" + civil_case.Text + "\\" + civil_case.Text + "." + Ext)) {messagebox. Show ("exists"); File.Delete (@ "Uploads \\" + civil_case.Text + "\\" + civil_case.Text + "." + Ext); File. Copy (upload_label 1 text, @ "upload \\" + civil_case.Text + "\\" + civil_case.Text + "." + Ext, true); } And (file copy (upload_label 1 text, @ "upload \\" + civil_case.Text + "\\" + civil_case.Text + "." + Ext, true);}} hold (exception eeee) {Message Box Try {string ext1 = upload_label2.Text; Ext1 = ext1.Substring (ext1.Length - 3, 3); if (file.exists ("upload" \ "" + civil_case.Text + "\\" + civil_case.Text + "_part2." + Ext1)) {Message Box Show ("Exists 2"); Delete File (@ "Upload \\" + civil_case.Text + "\\" + civil_case .ext + "." + Ext1); File copy (upload_label2.Text, @ "uploads \\" + civil_case.Text + "\\" + civil_case.Text + "_part2." + Ext1, true);} and (File copy (upload_label2 text, @ "upload \\" + civilcuse.text + "\\" + CivicCase. Lesson + "_part2." + Ext1, true);}} hold (exception eeee1) {message box. Show (eeee1 + "");} MessageBox.Show ("Updated!");} .
I do not see any problems in the two upload button events However, there is a bug in the second attempt / catch block in your update button:
File.Delete (@ "uploads \\" + civil_case.Text + "\ \" + Civil_case.Text + ". "+ Ext1); In the first block, you create the same string in four places:
@" upload \\ "+ civil_case.Text + "\\" + Civil_case.Text + "." In the second section, you create this string in three places, but do not miss the fourth: @ "upload \\" + Civil_case text + "\\" + civil_case.Text + "_part2." + Ext1 Therefore, your second attempt / grip is already deleting the file, so only the second file is finally Are you looking at what this match is?
Since you mentioned the lack of C # / OOP experience, there is a slightly better way to break the upload button code (as an example):
/// & lt; Summary & gt; /// Show a file dialog and update a label if the name of the dialog file comes back. /// & lt; / summary & gt; /// & lt; Ultimate name = "label" & Labels for updating. & Lt; / param & gt; Private Zero ShowFileDialogAndUpdateLabel (label label) {var file = new OpenFileDialog {title = "Browse file", Filter = "PDF files (*. PDF) | *. PDF | Images (* .mpp; *. JPEG; * .JPG; * .GIF; *. PNG; *. TIFF * * .mpp; *. JPEG; *. JPG; *. GIF; * PNG; * TIFF | all files (*. *) * *. * "}; If (file.ShowDialog () == DialogResult.OK) label.text = path.getFullPath (file.FileName);} Private Zero upload_1_click (object sender, eventAggas E) {showfileDialog and update label (upload_label 1); } Private Zero upload_2_ Click (Object Sender, EventArgs e) {ShowFileDialogAndUpdateLabel (upload_label2);}
Comments
Post a Comment