Hi all. Question about directories i have to send this file to my teacher via email then he will install the app. My problem is if i give him this code ( which work fine on my computer) i have no way of know what drive he will install this on which will stop the program from run how can i get around this.


const string FILENAME = @"C:\Users\michael\Documents\data.txt";


My Code:

namespace WriteFriendRecords
{
    public partial class Form1 : Form
    {
        const string Record = ",";
        const string FILENAME = @"C:\Users\michael\Documents\data.txt";
        string firstName, lastName, birthMonth;
        int phoneNoArea, phoneNo, birthDay;
        static FileStream outFile = new FileStream(FILENAME, FileMode.Create, FileAccess.Write);
        StreamWriter Writer = new StreamWriter(outFile);

        public Form1()
        {
            InitializeComponent();
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            firstName = txtFirstName.Text;
            lastName = txtLastName.Text;
            phoneNoArea = Convert.ToInt32(txtAreaCode.Text);
            phoneNo = Convert.ToInt32(txtPhoneNumber.Text);
            birthMonth = txtBirthMonth.Text;
            birthDay = Convert.ToInt32(txtDateOfBirth.Text);
            Writer.WriteLine(firstName + Record + lastName + Record + phoneNoArea + Record + phoneNo + Record + birthMonth + Record + birthDay + Record);

            // Clear the Text Boxes
            txtFirstName.Clear();
            txtLastName.Clear();
            txtAreaCode.Clear();
            txtPhoneNumber.Clear();
            txtBirthMonth.Clear();
            txtDateOfBirth.Clear();
            MessageBox.Show("File Saved");
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Writer.Close();
            outFile.Close();

            if (Disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(Disposing);
            Application.Exit();
        }
    }
}

Thanks i have used dialog box before so that should not be aproblem to me but what about saving the file to a directory

I'm confused about your question. Do you want to know how to actually write the file, or how to select the path of the file?

Yeah but i was hard coding in the path for the file @(c:\Blaa\blaa.txt);

i can't save it to a directory that my teacher don't have

Reading the file would be ok with the dialog box or am i wrong

You can identify your teachers home drive with:

Environment.GetEnvironmentVariable("HomeDrive").

For your hard coded location alternative try:

Environment.GetFolderPath(Environment.SpecialFolders.MyDocuments) + "\\data.txt"

Edited 5 Years Ago by iconoclazt: n/a

This question has already been answered. Start a new discussion instead.