Home > Back-end >  How to handle a case when the hard disk drive letter is not the same when saved it to a text file?
How to handle a case when the hard disk drive letter is not the same when saved it to a text file?

Time:02-06

in the constructor :

    SaveLoadFiles.LoadFile(textBoxRadarPath, "radarpath.txt");
SaveLoadFiles.LoadFile(textBoxSatellitePath, "satellitepath.txt");

if (textBoxRadarPath.Text != "" || textBoxSatellitePath.Text != "")
            {
                if(!Directory.Exists(textBoxRadarPath.Text))
                {
                    Directory.CreateDirectory(textBoxRadarPath.Text);
                }

                if (!Directory.Exists(textBoxSatellitePath.Text))
                {
                    Directory.CreateDirectory(textBoxSatellitePath.Text);
                }

                btnStart.Enabled = true;
            }
            else
            {
             btnStart.Enabled = false;
            }

the SaveLoadFiles class

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Weather
{
    public class SaveLoadFiles
    {
        public static void SaveFile(string contentToSave, string fileName)
        {
            string applicationPath = Path.GetFullPath(System.AppDomain.CurrentDomain.BaseDirectory); // the directory that your program is installed in
            string saveFilePath = Path.Combine(applicationPath, fileName);
            File.WriteAllText(saveFilePath, contentToSave);
        }

        public static void LoadFile(TextBox loadTo, string fileName)
        {
            string applicationPath = Path.GetFullPath(System.AppDomain.CurrentDomain.BaseDirectory); // the directory that your program is installed in
            string saveFilePath = Path.Combine(applicationPath, fileName); // add a file name to this path.  This is your full file path.

            if (File.Exists(saveFilePath))
            {
                loadTo.Text = File.ReadAllText(saveFilePath);
            }
        }
    }
}

when i used the application before and backed it up on my usb flash drive the second hard drive letter was D i had two hard disks : C and D and the project and the folders were on drive D.

now i backed up the project including the saved files but now my hard disks letters are C and E there is no D

but in the constructor when it's reading the text files the folders in the text files are D:....etc

but it should be E:

I'm checking if the folder exist or not and then if not creating it but it's trying to create the folder on drive D and D is not existing.

CodePudding user response:

You are reading contents of a data file that has a file path that no longer exists.

The solution is to edit those data files: "radarpath.txt" and "satellitepath.txt" to have the proper path.

An application would normally provide a UI for selecting the folder to use, rather than saving a hardcoded path in a datafile. What you could do is use FileDialog to prompt the user for the directories to use if they don't exist.

  • Related