Dinav Ahire
Dinav Ahire

Reputation: 583

how to store file in drive that is available in target pc while installing winforms c# application

I have winforms c# project and in that I have two .txt files i.e., credentials.txt and RetailButton_Exception.txt. Now I have given path to D: drive at development side. Now what if I install my application in different pc which does not have D: ?

I have given the code for saving these files as follow:-

 private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {

                string Log_API = "http://api.retailbutton.co/WS/Service.php?Service=employeeLogin";
                if (LoginUser(Log_API))
                {
                    logIn_Status = "true";
                    GlolbalUtil.authenticate = "true";
                    GlolbalUtil.LogIn_Status = logIn_Status;
                    this.Hide();

                    //string credentialPath = @"D:\credentials.txt";
                    String test = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
                    if (File.Exists(test + "credentials.txt"))
                    {
                        using (StreamWriter writer = new StreamWriter(test, true))
                        {
                            //writer.WriteLine("UserName :" + txtUsername.Text + Environment.NewLine + "Password :" + txtPassword.Text);
                            writer.WriteLine(txtUsername.Text);
                            writer.WriteLine(txtPassword.Text);
                        }

                        frmDash frmDash = new frmDash();
                        frmDash.Owner = this;
                        frmDash.Show();
                        txtUsername.Text = "";
                        txtPassword.Text = "";
                    }
                    else
                    {
                        using(FileStream fs = File.Create(test))
                        {
                            #region
                            using (StreamWriter writer = new StreamWriter(test, true))
                            {
                                //writer.WriteLine("UserName :" + txtUsername.Text + Environment.NewLine + "Password :" + txtPassword.Text);
                                writer.WriteLine(txtUsername.Text);
                                writer.WriteLine(txtPassword.Text);
                            }

                            frmDash frmDash = new frmDash();
                            frmDash.Owner = this;
                            frmDash.Show();
                            txtUsername.Text = "";
                            txtPassword.Text = "";
                            #endregion
                        }
                        #region
                        //using (StreamWriter writer = new StreamWriter(credentialPath, true))
                        //{
                        //    //writer.WriteLine("UserName :" + txtUsername.Text + Environment.NewLine + "Password :" + txtPassword.Text);
                        //    writer.WriteLine(txtUsername.Text);
                        //    writer.WriteLine(txtPassword.Text);
                        //}

                        //frmDash frmDash = new frmDash();
                        //frmDash.Owner = this;
                        //frmDash.Show();
                        //txtUsername.Text = "";
                        //txtPassword.Text = "";
                        #endregion
                    }
                    //GlolbalUtil.accept_status = "1";
                }
                else
                {
                    MessageBox.Show("Please Check Username and password");

                }


            }
            catch (Exception ex)
            {
                string filePath = @"D:\RetailButton_Exception.txt";

                using (StreamWriter writer = new StreamWriter(filePath, true))
                {
                    writer.WriteLine("Message :" + ex.Message + "<br/>" + Environment.NewLine + "StackTrace :" + ex.StackTrace +
                       "" + Environment.NewLine + "Date :" + DateTime.Now.ToString());
                    writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
                }
            }
        }

Upvotes: 0

Views: 80

Answers (4)

norekhov
norekhov

Reputation: 4243

The answer is simple - you shouldn't use absolute path. Choose one of specific system folders depending on what you're doing. Check folders here I suggest using ApplicationData if it's per user files. Or use CommonApplicationData if it's per system files.

That's how most of the applications behave now. Don't try to store app created files in your installation folder cause it's not per user. It's per machine.

It may be even prohibited for your app to write to Program Files cause it's folder is not intended to store application configuration.

An example from msdn on how to use SpecialFolders.

// Sample for the Environment.GetFolderPath method 
using System;

class Sample 
{
    public static void Main() 
    {
    Console.WriteLine();
    Console.WriteLine("GetFolderPath: {0}", 
                 Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
    }
}

Sample how to store info there:

    static void Main(string[] args)
    {
        var file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "userinfo.txt");
        using (var writer = new StreamWriter(file))
        {
            writer.WriteLine("Hello, World!");
        }
    }

Upvotes: 2

hdkhardik
hdkhardik

Reputation: 662

Just use your application folder, you can use it by Application.StartupPath

try doing this

string yourfilepath = Application.StartupPath + "credentials.txt";
string secondfile = Application.StartupPath + "RetailButton_Exception.txt";

Upvotes: 0

Yogi
Yogi

Reputation: 9739

You can also use Application.StartupPath for building you path, it is the path where you executable is placed.

        string credentialFilePath = Path.Combine(Application.StartupPath, "credentials.txt");
        string retailExceptionFilePath = Path.Combine(Application.StartupPath, "RetailButton_Exception.txt");

Upvotes: 2

Yeldar Kurmangaliyev
Yeldar Kurmangaliyev

Reputation: 34189

You can place your txt files in the same folder with your compiled exe file.

Then, you can use a relative path like this:

File.WriteAllText(@"credentials.txt", String.Empty);

If your application will be installed on:

C:\Program Files\YourApplication\yourapplication.exe

Then it will try to open

C:\Program Files\YourApplication\credentials.txt

Moreover, you can add your txt files to a Visual Studio project and set a property Copy To Output Directory to Copy if newer.
After that your output directory will always have these two files and it will be easier for you to work with it.

Upvotes: 0

Related Questions