i am a newbie to programming. I have been working to make a Readme.txt generator. it is working, but i am having trouble finishing it. My problem is that i cannot get it to not overwrite the existing readme. I have it so that it will generate the readme file and save it to the desktop but when the file is already there, i tell the user that a file already exists, and ask if they want to overwrite. if you choose no, it still overwrites.
if ((text1.Equals("")) || (text4.Equals("")) || (text6.Equals("")) || (GameSet == false) || (MapSize == false)) //this line tells the system to not generate the readme if these errors occur return; string strFile = "ReadMe.txt"; if (File.Exists(strFile))
{
DialogResult dr = MessageBox.Show("A ReadMe file already exists here. do you want to overwrite it?", "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation); if (dr == DialogResult.No){
MessageBox.Show("the file was not overwritten"); return;}
else MessageBox.Show("The ReadMe has been successfully Generated", "Done!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);}
}
jamiePosted Jan 12, 2008, 1:18 AM
Scott LyslePosted Jan 12, 2008, 1:02 AM
Well, just to keep things clean and tidy, why not move all of the code used to write the readme file into a separate method and then only call that method if the user okays the dialog to allow it to overwrite the file.
By way of a simple example:
private void button1_Click(object sender, EventArgs e) { if (System.IO.File.Exists("C:\\Temp\\ReadMe.txt")) { DialogResult dl = MessageBox.Show("A ReadMe file already exists here. Do you want to overwrite it?", "File Exists", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation); if (dl == DialogResult.No) { MessageBox.Show("OK. No file for you, Jack"); return; } else { WriteTheFile(); } } else { // write the file WriteTheFile(); } } private void WriteTheFile() { // write the file using your existing code MessageBox.Show("Writing files here"); }jamiePosted Jan 12, 2008, 12:27 AM
I have been trying to figure this out for two days. I have moved that code all over the place and back i have already spent another 4 hours on it tonight with no luck. i tried a new bit. it's not overwriting now, but if i choose yes to overwrite, it does nothing. this is what i am working on now. At the top of the method
if (File.Exists("ReadMe.txt")){
DialogResult dl = MessageBox.Show("A ReadMe file already exists here. Do you want to overwrite it?", "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation); if(dl == DialogResult.No) return;}
else{
//else is all the code
Scott LyslePosted Jan 12, 2008, 12:20 AM
It looks like you'd want to move your test to see if the file exists and the dialog asking the user if they want to overwrite an existing file to top of the generate readme method and then exit there before the file is generated if the user does not want to overwrite an existing file.
jamiePosted Jan 12, 2008, 12:08 AM
You lost me.... :-)
[code]
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;namespace
WindowsApplication1{
public partial class Form1 : Form{
public Form1(){
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e){
Width = 1000;
Height = 600;
StartPosition =
FormStartPosition.CenterParent;FormBorderStyle =
FormBorderStyle.FixedDialog;MaximizeBox =
false;}
private void label2_Click(object sender, EventArgs e){
}
private void label4_Click(object sender, EventArgs e){
}
private void checkBox2_CheckedChanged(object sender, EventArgs e){
}
private void label9_Click(object sender, EventArgs e){
}
private void label13_Click(object sender, EventArgs e){
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e){
MessageBox.Show("CyberShots ReadMe Generator. Version 1", "About The Generator", MessageBoxButtons.OK, MessageBoxIcon.Information);}
private void button2_Click(object sender, EventArgs e){
this.Close();}
private void GenerateReadMe_Click(object sender, EventArgs e){
}
private void GenerateReadMe_Click_1(object sender, EventArgs e){
StreamWriter OutFile = File.CreateText("ReadMe.txt"); string text1, text2, text3, text4, text5, text6; // assing the variables for the textboxestext1 = textBox1.Text;
// assign the text variable to a the textboxestext2 = textBox2.Text;
text3 = textBox3.Text;
text4 = textBox4.Text;
text5 = textBox5.Text;
text6 = textBox6.Text;
bool checked1 = checkBox1.Checked; //assign the checkboxes to a variable bool checked2 = checkBox2.Checked; bool checked3 = checkBox3.Checked; bool checked4 = checkBox4.Checked; bool checked5 = checkBox5.Checked; bool checked6 = checkBox6.Checked; bool checked7 = checkBox7.Checked; bool checked8 = checkBox8.Checked; bool checked9 = checkBox9.Checked; bool checked10 = checkBox10.Checked; bool checked11 = checkBox11.Checked; bool checked12 = checkBox12.Checked; bool checked13 = checkBox13.Checked; bool checked14 = checkBox14.Checked; if (text1.Equals("")) // if the information entered into these texboxes is blank{
MessageBox.Show("You must enter the authors name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return;}
if (text4.Equals("")){
MessageBox.Show("You must enter the map title", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return;}
if (text6.Equals("")){
MessageBox.Show("You must enter the associated game", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return;}
DateTime date;date =
DateTime.Now;OutFile.WriteLine(
"***********************************************************************");OutFile.WriteLine(
"* This ReadMe Generated by *");OutFile.WriteLine(
"* CyberShot's ReadMe Generator *");OutFile.WriteLine(
"* www.CybersMods.com *");OutFile.WriteLine(
"* www.asp-gamers.com *");OutFile.WriteLine(
"* This File was generated on " + date + " *");OutFile.WriteLine(
"***********************************************************************");OutFile.WriteLine(
"Authors Name: " + text1);OutFile.WriteLine();
//to create blank spaces between the infoOutFile.WriteLine(
"Authors Email: " + text2);OutFile.WriteLine();
OutFile.WriteLine(
"Authors Website: " + text3);OutFile.WriteLine();
OutFile.WriteLine(
"Map Title: " + text4);OutFile.WriteLine();
OutFile.WriteLine(
"Map Version: " + text5);OutFile.WriteLine();
OutFile.WriteLine(
"Associated Game: " + text6);OutFile.WriteLine();
OutFile.WriteLine(
"***********************************************************************");OutFile.WriteLine(
"* *");OutFile.WriteLine(
"* Supported Gametypes *");OutFile.WriteLine(
"* *");OutFile.WriteLine(
"***********************************************************************");OutFile.WriteLine();
bool GameSet = false; bool[] checking = new bool[14] { checked1, checked2, checked3, checked4, checked5, checked6, checked7, checked8, checked9, checked10, checked11, checked12, checked13, checked14 }; for (int count = 0; count < checking.Length; count++){
if (checking[count] == true)GameSet =
true;}
if (GameSet == false){
MessageBox.Show("you must enter a gametype", "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation); return;}
else if (checked1)OutFile.WriteLine(
"Capture the flag"); if (checked2)OutFile.WriteLine(
"Behind Enemy Lines"); if (checked3)OutFile.WriteLine(
"Multyplayer"); if (checked4)OutFile.WriteLine(
"Team Death Match"); if (checked5)OutFile.WriteLine(
"DeathMatch"); if (checked6)OutFile.WriteLine(
"Free For All"); if (checked7)OutFile.WriteLine(
"Domination"); if (checked8)OutFile.WriteLine(
"Sabotage"); if (checked9)OutFile.WriteLine(
"Infiltration"); ; if (checked10)OutFile.WriteLine(
"Demolition"); if (checked11)OutFile.WriteLine(
"Headquarters"); if (checked12)OutFile.WriteLine(
"Single Player"); if (checked13)OutFile.WriteLine(
"Retrival"); if (checked14)OutFile.WriteLine(
"Other");OutFile.WriteLine();
bool checked15 = checkBox15.Checked; bool checked16 = checkBox16.Checked; //checks to see if the checkbox has been checked bool checked17 = checkBox17.Checked; bool checked18 = checkBox18.Checked; bool checked19 = checkBox19.Checked; bool MapSize = false; bool[] Size = new bool[5] { checked15, checked16, checked17, checked18, checked19 }; for (int count = 0; count < Size.Length; count++){
if (Size[count] == true)MapSize =
true; //check to see if the map size buttons have been checked}
if (MapSize == false){
MessageBox.Show("you must enter the map size", "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation); return;}
elseOutFile.WriteLine(
"***********************************************************************");OutFile.WriteLine(
"* *");OutFile.WriteLine(
"* Map Size *");OutFile.WriteLine(
"* *");OutFile.WriteLine(
"***********************************************************************");OutFile.WriteLine();
if (checked15)OutFile.WriteLine(
"2 - 5 Players"); if (checked19)OutFile.WriteLine(
"5 - 10 Players"); if (checked16)OutFile.WriteLine(
"10 - 15 Players"); if (checked17)OutFile.WriteLine(
"15 - 20 Players"); if (checked18)OutFile.WriteLine(
"25 Players and up");OutFile.WriteLine();
string ConstructionTime;ConstructionTime = textBox10.Text;
if (ConstructionTime.Equals(""))OutFile.Close();
else{
OutFile.WriteLine(
"***********************************************************************");OutFile.WriteLine(
"* *");OutFile.WriteLine(
"* Construction Time *");OutFile.WriteLine(
"* *");OutFile.WriteLine(
"***********************************************************************");OutFile.WriteLine();
OutFile.WriteLine(ConstructionTime);
OutFile.WriteLine();
}
string instructions, content, thanks, notes;instructions = textBox7.Text;
content = textBox8.Text;
thanks = textBox9.Text;
notes = textBox11.Text;
if (instructions.Equals("")) //if no installation instructions are entered then it won't print this sectionOutFile.Close();
else{
OutFile.WriteLine(
"***********************************************************************");OutFile.WriteLine(
"* *");OutFile.WriteLine(
"* Installation Instructions *");OutFile.WriteLine(
"* *");OutFile.WriteLine(
"***********************************************************************");OutFile.WriteLine();
OutFile.WriteLine(instructions);
OutFile.WriteLine();
}
if (content.Equals("")) //if no content is entered, then it doesn't print the content sectionOutFile.Close();
else{
OutFile.WriteLine(
"***********************************************************************");OutFile.WriteLine(
"* *");OutFile.WriteLine(
"* Custom Content *");OutFile.WriteLine(
"* *");OutFile.WriteLine(
"***********************************************************************");OutFile.WriteLine();
OutFile.WriteLine(content);
OutFile.WriteLine();
}
if (thanks.Equals("")) //if no special thanks are entered, then it doesn't print the special thanks sectionOutFile.Close();
else{
OutFile.WriteLine(
"***********************************************************************");OutFile.WriteLine(
"* *");OutFile.WriteLine(
"* Special Thanks *");OutFile.WriteLine(
"* *");OutFile.WriteLine(
"***********************************************************************");OutFile.WriteLine();
OutFile.WriteLine(thanks);
OutFile.WriteLine();
}
if (notes.Equals("")) //if no notes are entered then it doesn't print the notes sectionOutFile.Close();
else{
OutFile.WriteLine(
"***********************************************************************");OutFile.WriteLine(
"* *");OutFile.WriteLine(
"* Additional Notes *");OutFile.WriteLine(
"* *");OutFile.WriteLine(
"***********************************************************************");OutFile.WriteLine();
OutFile.WriteLine(notes);
OutFile.Close();
}
if ((text1.Equals("")) || (text4.Equals("")) || (text6.Equals("")) || (GameSet == false) || (MapSize == false)) //this line tells the system to not generate the readme if these errors occur return;OutFile.Close();
string strFile = "ReadMe.txt"; if (File.Exists(strFile)){
DialogResult dr = MessageBox.Show("A ReadMe file already exists here. do you want to overwrite it?", "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation); if (dr == DialogResult.No){
MessageBox.Show("the file was not overwritten"); return;}
else{
MessageBox.Show("The ReadMe has been successfully Generated", "Done!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); this.Close();}
}
}
private void label14_Click(object sender, EventArgs e){
}
public void button1_Click(object sender, EventArgs e){
foreach (Control myControl in this.Controls){
TextBox isTextBox = myControl as TextBox; if (isTextBox != null){
isTextBox.Text =
"";}
else foreach (Control ctrl in this.Controls){
if (ctrl is CheckBox){
CheckBox cb = (CheckBox)ctrl;cb.Checked =
false;}
}
}
}
private void pictureBox2_Click(object sender, EventArgs e){
}
private void textBox1_TextChanged(object sender, EventArgs e){
}
private void saveFileDialog1_FileOk(object sender, CancelEventArgs e){
}
private void pictureBox2_Click_1(object sender, EventArgs e){
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e){
System.Diagnostics.
Process.Start("www.cybersmods.com");}
}
}
[/code]
Scott LyslePosted Jan 12, 2008, 12:04 AM
You handled the dialog correctly and it will return from this method after giving the user the message box stating that the file was not overwritten; I don't know where it goes from there but it must return to the calling method; the file is not written in the code you provided so is it that when it returns to the calling method that the file is actually written? If so, what have done to prevent it from being overwritten if the user decides not to overwrite the file?
If you set a break point on the message box provided when the user selects No from the dialog, and then step forward from that point on I would think that you'd see where the file is getting written.