JohnA
JohnA

Reputation: 594

How do I make the streamWriter write a new file in the same folder C# .NET

I have 3 forms.. Only 2 which I am using. The registerForm, loginForm and mainForm.

This is how everything is set up.

Application opens and prompts the user with the login form > User can either log in to a existing account or press register > user logs in > streamWriter creates a text file with the users username and password > textfile stores at ("C:\" + regUsernametextBox.Text + "\infoBox.creds"). Keep in mind that the ("infoBox.creds") is just an extension that I made.

User can type stuff into the mainForm's textbox which is called textBox1

I want the streamWriter to write a textfile of the content that is inside of the textBox1 into the same folder as the user credentials are.

Example

I create a user named Tom > The application creates a folder names Tom with a textfile in it, and that textfile contains the username and password of the user.

When the user that is logged in writes something in the textBox1 I want it to save that text to another textfile but in the same folder which would be the "Tom" folder.

What I have tried

What I have done (as you can see below) is that i've tried storing the value of the username from the loginForm and tried using that value in my mainForm

Giving the file a name

var streamWrite = new System.IO.StreamWriter("C:\\" + loginFrm.folderName + "\\Conent.info");

And then storing the value in it

    System.IO.Directory.CreateDirectory("C:\\" + loginFrm.folderName);
    var streamWrite = new System.IO.StreamWriter("C:\\" + loginFrm.folderName + "\\Conent.info");
    streamWrite.Write(textboxContent);

But it doesnt write a textfile with the information that has been given to the textBox1 in the mainForm.

I hope I described my issue well and I appriciate any help possible!



mainForm Code

public partial class mainForm : MetroForm
{
    public mainForm()
    {
        InitializeComponent();
    }
    public string textboxContent;

    private void Form1_Load(object sender, EventArgs e)
    {
        textbox1.Text = Properties.Settings.Default.SavedText;
        loginForm loginFrm = new loginForm();
        loginFrm.Hide();
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        loginForm loginFrm = new loginForm();
        textboxContent = textbox1.Text;
        try
        {
            var streamWrite = new System.IO.StreamWriter("C:\\" + loginFrm.folderName + "\\Conent.info");
            streamWrite.Write(textboxContent);
            streamWrite.Close();
        }
        catch(System.IO.DirectoryNotFoundException ex)
        {
            System.IO.Directory.CreateDirectory("C:\\" + loginFrm.folderName);
            var streamWrite = new System.IO.StreamWriter("C:\\" + loginFrm.folderName + "\\Conent.info");
            streamWrite.Write(textboxContent);
            streamWrite.Close();
            MessageBox.Show("Nope");

        }

loginForm Code

        public loginForm()
        {
            InitializeComponent();
        }

        public string username, password;
        public string folderName;

        private void button1_Click(object sender, EventArgs e)
        {

            try
            {
                folderName = username;
                var sr = new System.IO.StreamReader("C:\\" + regUsernametextBox.Text + "\\infoBox.creds");
                username = sr.ReadLine();
                password = sr.ReadLine();
                sr.Close();

                if(username == regUsernametextBox.Text && password == regPasswordTextBox.Text)
                {
                    MessageBox.Show("You have successfully logged in", "Authorize");

                    loginForm regFrm = new loginForm();
                    this.Hide();
                    mainForm mainFrm = new mainForm();
                    mainFrm.ShowDialog();
                    this.Show();
                }
                else
                {
                    MessageBox.Show("Please make sure you typed int he correct name and password", "Authorize Error!");
                }

            }
            catch(System.IO.DirectoryNotFoundException ex)
            {
                MessageBox.Show("The user does not exist","Error");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void registerLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            registerForm regFrm = new registerForm();
            regFrm.Visible = true;


        }
    }
}

registerForm Code

        public registerForm()
        {
            InitializeComponent();
        }

        private void registerForm_Load(object sender, EventArgs e)
        {

        }

        private void registerButton_Click(object sender, EventArgs e)
        {
            try
            {
                var sw = new System.IO.StreamWriter("C:\\" + regUsernametextBox.Text + "\\infoBox.creds");
                sw.Write(regUsernametextBox.Text + "\n" + regPasswordTextBox.Text);
                sw.Close();
            }
            catch(System.IO.DirectoryNotFoundException ex)
            {
                System.IO.Directory.CreateDirectory("C:\\" + regUsernametextBox.Text);
                var sw = new System.IO.StreamWriter("C:\\" + regUsernametextBox.Text + "\\infoBox.creds");
                sw.Write(regUsernametextBox.Text + "\n" + regPasswordTextBox.Text);
                MessageBox.Show("Account successfully created!","Account");
                sw.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

Upvotes: 1

Views: 3121

Answers (1)

Mong Zhu
Mong Zhu

Reputation: 23732

When your mainForm is closing

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    loginForm loginFrm = new loginForm();

Your create a new loginForm, Which is blank and the value loginFrm.folderName should be null or an empty string (I don't see the constructor of loginForm).

You need the original reference of the loginForm with the entered data, or at least the folderName.

So in your mainForm create a variable that can catch the folder name

public partial class mainForm : MetroForm
{
    public string User_folder_Name;

When you call the mainForm in the loginForm pass first the value:

mainForm mainFrm = new mainForm();

mainFrm.User_folder_Name = folderName;

mainFrm.ShowDialog();

And now you can use it in the closing event of your mainForm. I would also suggest using a "using" block.

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    loginForm loginFrm = new loginForm();
    textboxContent = textbox1.Text;
    // Path.Combine is a conmfortable tool to create paths
    string completeFolderPath = Path.Combine(@"C:\", User_folder_Name);
    string completeFIlePath = Path.Combine(completeFolderPath, "Conent.info");


    using (StreamWriter writer = new StreamWriter(completeFIlePath))
    {   // check whether the directory exists and create on if NOT
        if (!Directory.Exists(completeFolderPath))
        {
            Directory.CreateDirectory(completeFolderPath);
            MessageBox.Show("Directory created");

        }
        // write the content
        streamWrite.Write(textboxContent);
    }    
}

EDIT:

in your loginForm you have the three variables

public string username, password;
public string folderName;

when the form is created with the new keyword. All those values are null in the beginning, because you don't assign explicitly values to them.

In the button click event you do this:

folderName = username;

which is basically assigning a variable which is null to another variable which is also null. So the result is null when you then read the file you fill only username but the value is never passed to foldername, which is still null.

Please exchange the order of the two lines. like this:

var sr = new System.IO.StreamReader("C:\\" + regUsernametextBox.Text + "\\infoBox.creds");
username = sr.ReadLine();
password = sr.ReadLine();

folderName = username;
sr.Close();

Upvotes: 2

Related Questions