简体   繁体   English

如何获取文件所在的路径,而不是获取调试文件夹的路径?

[英]How to obtain the path in which a file is residing in instead of getting the path to the debug folder?

I'm Currently inside a for loop which iterates through a set of folders and obtains some required values from a JSON file. 我目前在for循环中,该循环遍历一组文件夹并从JSON文件中获取一些必需的值。 I also want to get the absolute path to each of those files. 我也想获取每个文件的绝对路径。

Currently I tried approaches such as 目前,我尝试了以下方法

string currentDir = AppDomain.CurrentDomain.BaseDirectory; 字符串currentDir = AppDomain.CurrentDomain.BaseDirectory; string currentDir = Directory.GetCurrentDirectory(); 字符串currentDir = Directory.GetCurrentDirectory();

Both of these gave me location to the debug file 这些都给了我调试文件的位置

what I want is the location of the Folder in which this file is existing rather . 我想要的是这个文件所在的文件夹的位置。

Below is the code segment to which I hope to include this new code. 以下是我希望将此新代码包括在其中的代码段。

string rootDirectory = fbd.SelectedPath; 字符串rootDirectory = fbd.SelectedPath;

var foundFiles = Directory.EnumerateFiles(rootDirectory, "server.config", SearchOption.AllDirectories); var foundFiles = Directory.EnumerateFiles(rootDirectory,“ server.config”,SearchOption.AllDirectories);

foreach (var file in foundFiles) { RepositoryHomeSettingsModel repositoryHomeSettingsModel = JsonConvert.DeserializeObject(File.ReadAllText(file)); foreach(foundFiles中的var文件){RepositoryHomeSettingsModel repositoryHomeSettingsModel = JsonConvert.DeserializeObject(File.ReadAllText(file));

string Name = SettingsModel.name; 字符串名称= SettingsModel.name;

} }

Please find below possible ans,Do you require the same ? 请在下面找到可能的答案,您是否需要同样的答案?

var foundFiles = Directory.EnumerateFiles(rootDirectory, "*.txt", SearchOption.AllDirectories);

            foreach (var file in foundFiles)
            {
                var folderpath= Path.GetDirectoryName(file);
                //RepositoryHomeSettingsModel repositoryHomeSettingsModel = JsonConvert.DeserializeObject(File.ReadAllText(file));

                //string Name = SettingsModel.name;

            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM