如何使用c#

时间:2016-01-15 08:22:33

标签: c# dictionary io key-value-store

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

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数据并将这些值存储到密钥名称文件夹中。由于我处理这种情况非常新,我没有得到正确的逻辑来执行此操作。然而,我尝试以下面的方式创建密钥名称文件夹,并且只有一个子文件夹。但我需要在密钥文件夹中创建所有值文件夹。我怎么能这样做。

我从中读取文本文件作为键值的POI类是 -

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

这是我运行程序时的代码输出。 Window form picture

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

Text_Image_withHash

现在,如果我打开文件夹,我可以看到以下文件夹,这是文本文件中的键值 Key Value folder

在组合框2的启用按钮2之后,生成了值文件夹但不在密钥文件夹中。但是在Text_Image_withHash中通常的方式。

key and values are  generated in the same folder where

但我想做的是 - enter image description here

2 个答案:

答案 0 :(得分:0)

要创建这种文件夹结构,只需使用foreach循环。并DEPLOY RESOURCE

必需的用法:

String.Split

示例:

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

然后可以通过以类似方式组合路径来简单地将JSON文件保存到这些目录中。

我还建议对您的路径进行一些清理(例如将// 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)); } '/'替换为'\\''_'

示例实施:

'-'

答案 1 :(得分:0)

我刚才得到了答案。这是上述问题的解决方案。

对于POI类:

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输出类

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

这是以这种方式生成的文件夹和文件 - imag1

image2 所有值的最终json文件 the final json file