处理情况的方法,当硬盘驱动器的字母在保存到文本文件时不相同时?

huangapple go评论65阅读模式
英文:

How to handle a case when the hard disk drive letter is not the same when saved it to a text file?

问题

在构造函数中:

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;
}

SaveLoadFiles类:

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); // 程序安装的目录
            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); // 程序安装的目录
            string saveFilePath = Path.Combine(applicationPath, fileName); // 在此路径添加文件名,这是完整的文件路径。

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

当我之前在应用程序上使用并将其备份到我的USB闪存驱动器上时,第二个硬盘驱动器字母是D,我有两个硬盘:C和D,项目和文件夹在D驱动器上。

现在我备份了项目,包括保存的文件,但现在我的硬盘驱动器字母是C和E,没有D。

但在构造函数中,当它读取文本文件时,文本文件中的文件夹是D:....等等,但它应该是E:

我正在检查文件夹是否存在,如果不存在则创建它,但它试图在D驱动器上创建文件夹,而D驱动器不存在。

英文:

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.

答案1

得分: 1

以下是要翻译的内容:

"radarpath.txt" 和 "satellitepath.txt" 这两个数据文件中包含了一个不再存在的文件路径。

解决方法是编辑这些数据文件,将文件路径修改为正确的路径。

通常,一个应用程序会提供用户界面,让用户选择要使用的文件夹,而不是将硬编码的路径保存在数据文件中。你可以使用 FileDialog 来提示用户选择要使用的目录,如果目录不存在的话。

英文:

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.

huangapple
  • 本文由 发表于 2023年2月6日 05:05:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定