如何使用 C# 将所有值存储在名为键的文件夹中

How to store all values in the key named folder using c#

我写了一个文本文件。此文本文件中每一行的第一项应该是键,其余项是值。我的文本文件看起来像这样-

Flensburg;Nordertor;Naval Academy Mürwik;Flensburg Firth
Kiel;Laboe Naval Memorial;Zoological Museum of Kiel University;Kieler Förde
Lübeck;Holstentor;St. Mary's Church, Lübeck;Passat (ship);Burgtor;Lübeck Museum of Theatre Puppets;Trave

为了我的项目目的,我需要为每个值创建 .json 数据并将这些值存储到键名中 folder.As 我是处理这种情况的新手,我没有得到正确的结果这样做的逻辑。但是,我尝试了以下方法,通过它我可以创建密钥名称文件夹,并且只在其中创建一个子文件夹。但我需要在密钥文件夹中创建所有值文件夹。我该怎么做。

我从中读取文本文件作为键值的兴趣点 class 是-

 public class POI
   {
    Dictionary<string, List<string>> poi = new Dictionary<string, List<string>>();
    public bool ContainsKey(string key) { return this.poi.ContainsKey(key); }
    public List<string> GetValue(string key) { return this.poi[key]; }

    public void POIList()
    {
        foreach (string line in File.ReadLines("POIList.txt"))
        {
            string[] parts = line.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            poi.Add(parts[0], new List<string>());
            poi[parts[0]] = new List<string>(parts.Skip(1));

         }

     }
 }

在form1.cs

   private void button1_Click(object sender, EventArgs e)
    {


            JSON_Output Json = new JSON_Output();

            Json.ToJsonForLocation(comboBox1.Text);

    }

我还从 combobox2 设置了 selectedindexchange

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (comboBox1.SelectedItem != null)
        {
            POI poi1 = new POI();
            poi1.POIList();
            string txt = comboBox1.SelectedItem.ToString();
            if (poi1.ContainsKey(txt))
            {
                List<string> points = poi1.GetValue(txt);
                comboBox2.Items.Clear();
                comboBox2.Items.AddRange(points.ToArray());

            }
        }
    }

现在生成的json文件痛值是-

        public void ToJsonForLocation(string name)
        {
         var startPath = Application.StartupPath;
        string folderName = Path.Combine(startPath, "Text_ImageWithHash");
        string SubfolderName = Path.Combine(folderName, name);
        //string folderName = Path.Combine(startPath, "Text_ImageWithHash");
        System.IO.Directory.CreateDirectory(SubfolderName);
        string fileName = name + ".json";

        var path = Path.Combine(SubfolderName, fileName);
        var Jpeg_File = new DirectoryInfo(startPath + @"\Image\" + name).GetFiles("*.jpg");

        POIData Poi=new POIData();
        Poi.Shorttext = File.ReadAllText(startPath + @"\Short Text\" + name + ".txt");
        Poi.GeoCoordinates=GeosFromString(startPath + @"\Latitude Longitude\" + name + ".txt");
        Poi.Images=new List<string> { Jpeg_File[0].Name};


        string json = JsonConvert.SerializeObject(Poi,Formatting.Indented);
        File.WriteAllText(path , json);
    }

这是我在 运行 程序时的代码输出。

点击按钮 1 后 Text_image_withHash 文件夹在配置目录中生成。

现在,如果我打开文件夹,我可以看到以下文件夹,它们是文本文件中的键值

在组合框 2 启用按钮 2 后,会生成值文件夹,但不会像往常一样在键 folder.but 中生成 Text_Image_withHash。

但是我想做的是-

要创建那种 folder-structure,只需使用 foreach-loop。还有 String.Split.

需要使用:

using System;
using System.IO;
using System.Text;
using System.Linq;

示例:

// basePath can be anything
var basePath = "C:\Something";
// assume "info" is your CSV.
var infoParts = info.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

if (infoParts.Length == 0)
{
    return;
}

var rootPath = infoParts[0];

Directory.CreateDirectory(Path.Combine(basePath, rootPath));

foreach (var subPath in infoParts.Skip(1))
{
    Directory.CreateDirectory(Path.Combine(basePath, rootPath, subPath));
}

将 JSON-files 保存到这些目录中可以简单地通过以类似方式组合路径来实现。

我还建议对您的路径进行一些清理(例如将 '/''\' 替换为 '_''-'

示例实现:

public void POIList()
{
    foreach (string line in File.ReadLines("POIList.txt"))
    {
        string[] parts = line.Split(new[] { ';' }, 
            StringSplitOptions.RemoveEmptyEntries);

        if (parts.Length == 0)
        {
            // Empty line or similar.
            continue;
        }

        string cityName = parts[0];

        poi.Add(cityName, new List<string>());

        // Add the points of interest to local.
        var points = new List<string>(parts.Skip(1));
        poi[cityName] = points;

        // basePath will have to be retrieved somehow. It's up to you.
        string cityDirectoryPath = Path.Combine(basePath, cityName));

        // Create a directory for the city.
        Directory.CreateDirectory(cityDirectoryPath);

        // Create sub-directories for points.
        foreach (string point in points)
        {
            Directory.CreateDirectory(Path.Combine(
                cityDirectoryPath, point));
        }
    }
}

我刚刚得到答案。这就是上面问题的解答。

兴趣点 Class :

public class POI
{
    Dictionary<string, List<string>> poi = new Dictionary<string, List<string>>();
    public bool ContainsKey(string key) { return this.poi.ContainsKey(key); }
    public List<string> GetValue(string key) { return this.poi[key]; }

    public void POIList()
    {
        foreach (string line in File.ReadLines("POIList.txt"))
        {
            string[] parts = line.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

            if (parts.Length == 0)
            {
                // Empty line or similar.
                continue;
            }

            string cityName = parts[0];

            poi.Add(cityName, new List<string>());

            // Add the points of interest to local.
            var points = new List<string>(parts.Skip(1));
            poi[cityName] = points;

            var startPath = Application.StartupPath;
            string folderName = Path.Combine(startPath, "FinalJson");
            string cityDirectoryPath = Path.Combine(folderName, cityName);

            Directory.CreateDirectory(cityDirectoryPath);

               }

    }
}

json输出class

 public void ToJsonForLocation(string CityName,string PoiName)
    {
        var startPath = Application.StartupPath;
        string folderName = Path.Combine(startPath, "FinalJson");
        string SubfolderName = Path.Combine(folderName, CityName);
        System.IO.Directory.CreateDirectory(SubfolderName);
        string fileName = PoiName + ".json";

        var path = Path.Combine(SubfolderName, fileName);
        var Jpeg_File = new DirectoryInfo(startPath + @"\Image\" + PoiName).GetFiles("*.jpg");

        POIData Poi=new POIData();
        Poi.Shorttext = File.ReadAllText(startPath + @"\Short Text\" + PoiName + ".txt");
        Poi.GeoCoordinates = GeosFromString(startPath + @"\Latitude Longitude\" + PoiName + ".txt");
        Poi.Images=new List<string> { Jpeg_File[0].Name};

        string json = JsonConvert.SerializeObject(Poi,Formatting.Indented);
        File.WriteAllText(path,json);
    }

这是这样生成的文件夹和文件-

所有值的最终 json 文件