Home > OS >  How Read File Path From a .dat file then open the program When Pressing The Launch Button?
How Read File Path From a .dat file then open the program When Pressing The Launch Button?

Time:05-07

I have a problem Where I cant make my program automatically read the given file path inside the .dat and be ready to launch the program when pressing launch file without opening openFileDialog and choosing the program every time.

the code im using here is for the user to enter the file path for the first time then create a file path .dat file and it works with now issues.

using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                string path = Path.Combine(desktop, "LS\\Fail-SafePath.dat");
                openFileDialog.InitialDirectory = filePath;
                openFileDialog.Filter = " PlayGTAV (*.exe)|PlayGTAV.exe";
                openFileDialog.FilterIndex = 1;
                openFileDialog.RestoreDirectory = true;

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    filePath = openFileDialog.FileName;

                    var fileStream = openFileDialog.OpenFile();

                    using (StreamReader reader = new StreamReader(fileStream))
                    {
                         fileContent = reader.ReadToEnd();

                    }

                    using (StreamWriter sw = File.CreateText(path))
                    {
                        sw.WriteLine(filePath);

                    }

After that that i have a start button for it

private void panel21_MouseClick(object sender, MouseEventArgs e)
        {
            Process.Start(filePath);
        }

This works well when the user does it for the first time but now I want it to read that .dat file path automatically without having to ask the user for the file path every single time which I don't know how to do and need help with please.

I was thinking to do it like that: When Pressing the Launch button (After the first time) The Program Checks if the Fail-SafePath.dat Exists if Yes it reads the lines from it and starts the program from the given path without opening OpenFileDialog.

I'm Using Visual Studio, Windows Form.

CodePudding user response:

If it's a file that the application will always need, then something like you mentioned:

I was thinking to do it like that: When Pressing the Launch button (After the first time) The Program Checks if the Fail-SafePath.dat Exists if Yes it reads the lines from it and starts the program from the given path without opening OpenFileDialog.

Could work easily enough. You could have your application look for it in the default location, and if not there, have your user select it.

Another solution could be using something like Application Settings or User Settings, which are values persisted between executions of .NET projects.

Depending on your full application design, you could also have the file path and other settings stored in some database or other data storage. There are a lot of ways to accomplish this.

EDIT: To elaborate further on the Application Settings

The application settings are very easy to read and write to.

You just need to create the ones you want, before trying to use them.

They can be created by:

  1. Open Visual Studio.
  2. In Solution Explorer, expand the Properties node of your project.
  3. Double-click the .settings file in which you want to add a new setting. The default name for this file is Settings.settings.
  4. In the Settings designer, set the Name, Value, Type, and Scope for your setting. Each row represents a single setting.

To read from your settings:

this.FilePath= Properties.Settings.Default.FilePath;  

To write to and save the setting:

Properties.Settings.Default.FilePath= Path.GetFullPath("importantFilePath");  

Properties.Settings.Default.Save();  
  • Related