Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 读取/写入外部日志和配置文件(xml、txt):如何设置相对路径?_C#_File Io_Relative Path - Fatal编程技术网

C# 读取/写入外部日志和配置文件(xml、txt):如何设置相对路径?

C# 读取/写入外部日志和配置文件(xml、txt):如何设置相对路径?,c#,file-io,relative-path,C#,File Io,Relative Path,我正在编写的C程序需要访问一些XML文件和一个txt日志文件,它们存储在一个外部文件夹中。我想做的是将这些文件放到一个名为ConfigFiles的文件夹中,然后将ConfigFiles文件夹放到Visual Studio项目文件夹中,这样无论我从哪台计算机运行程序,它都能够访问这些文件。我还没有找到工作的相对路径。我应该如何设置 我试过: //1)Make sure config folder exist and start application Logging

我正在编写的C程序需要访问一些XML文件和一个txt日志文件,它们存储在一个外部文件夹中。我想做的是将这些文件放到一个名为ConfigFiles的文件夹中,然后将ConfigFiles文件夹放到Visual Studio项目文件夹中,这样无论我从哪台计算机运行程序,它都能够访问这些文件。我还没有找到工作的相对路径。我应该如何设置

我试过:

        //1)Make sure config folder exist and start application Logging
        if (!Directory.Exists(FFXIVFolderPath))
        {
            var appRoot = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            var fullPath = Path.Combine(appRoot, FFXIVFolderPath);

            var message = "FFXIV config directory (" + fullPath + ") does not exist, please select new folder...";
            Globals.Instance.ShowMessage(message);

            // Show the FolderBrowserDialog
            var folderBrowserDialog1 = new FolderBrowserDialog();
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                FFXIVFolderPath = folderBrowserDialog1.SelectedPath;
            }
            else {Application.Exit();}

            MemoryApi.SetForegroundWindow(Handle);
        }
更新我的解决方案: 我最终决定,我希望我的资源文件位于特殊文件夹文档中,在当前用户下,用户应该能够修改它们。这是我的代码,以防有人需要这样做:

     private const string FFXIVFolder = @"My Games\XYZ Game\Bot Config Files";
     public static string FFXIVFolderPath { get; private set; }

     private void MainForm_Load() {
        //1)Make sure config folder exist and start application Logging
        string documentsDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        FFXIVFolderPath = Path.Combine(documentsDir, FFXIVFolder);
        if (!Directory.Exists(FFXIVFolderPath))
        {
            var message = "FFXIV config directory (" + FFXIVFolderPath + ") does not exist, please select new folder...";
            Globals.Instance.ShowMessage(message);

            // Show the FolderBrowserDialog
            var folderBrowserDialog1 = new FolderBrowserDialog();
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                FFXIVFolderPath = folderBrowserDialog1.SelectedPath;
            }
            else
            {
                Application.Exit();
                return;
            }

            MemoryApi.SetForegroundWindow(Handle);
        }
        Globals.Instance.LoggerDictionary.Add("ApplicationLog", new Logger("ApplicationLog"));
     }

我认为,在检查时,Directory.ExistsFFXIVFolderPath这个FFXIVFolderPath应该包含完整的路径。计算完整路径var fullPath=path.combineaproot,FFXIVFolderPath;在这里,FFXIVFolderPath应该具有相对路径。i、 e.如果在目录存在之前计算完整路径,则在if条件中不需要此代码


有关制定相对路径的信息,请参考以下链接


请检查这个。。您可能需要在VisualStudio中编写并单击配置文件,然后将“复制本地”属性设置为True。如果这解决了您的问题,请让我知道我将添加此作为答案右键单击->属性->参考Tab@kishoreVMKishore,我只想在代码中设置一个相对路径,并将文件放在项目文件夹下的文件夹中。知道吗?我想在检查时,Directory.ExistsFFXIVFolderPath这个FFXIVFolderPath应该包含完整的路径。计算完整路径var fullPath=path.combineaproot,FFXIVFolderPath;在这里,FFXIVFolderPath应该具有相对路径。i、 e.如果您在目录存在之前计算完整路径,则在if条件中不需要此代码。要制定相对路径,请参考以下链接