Я хочу преобразовать представление данных в INI-файл после ввода данных - PullRequest
0 голосов
/ 23 апреля 2019

Я хочу преобразовать сетку данных в INI-файл после ввода данных.

http://www.hoons.net/Board/qacshap/Content/67073

Когда я ввожу вышеуказанный URL,

Я пытаюсь поместить данные в сетку и нажимаю кнопку экспорта, чтобы сохранить их как файл .ini в виде раздела, ключа, значения. Что я должен делать? Внутри кода контент создается как INI-файл, но не как сетка.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;

namespace EXPORT
{
    public partial class Form1 : DevExpress.XtraEditors.XtraForm
    {
        [DllImport('kernel32')]

        public static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        [DllImport('kernel32')]

        public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        static string path = 'C:\\Test.ini';
        public Form1()
        {
            InitializeComponent();
            dataGridView1.AllowUserToAddRows =true; //자동 행 추가
            dataGridView1.AutoGenerateColumns = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            WritePrivateProfileString('SECTION', 'KEY', 'VALUE', @'C:\ConnectionInfo.ini');
            MessageBox.Show('EXPORT successfully to *.INI format');


        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {


        }
        private void WriteInFile(string section,string key,string value,string path)
        {
            WritePrivateProfileString(section, key, value, path);
            if (value == null)
            {
                throw new ArgumentException();
            }
        }

        private void button2_Click(object sender, EventArgs e)  //ADD_ROW Button
        {
            DataGridViewButtonColumn button = new DataGridViewButtonColumn();
            {
                dataGridView1.Rows.Add();
            }
        }
    }
}

1 Ответ

0 голосов
/ 24 апреля 2019

Вы можете использовать мою библиотеку MadMilkman.Ini для этого, вот как:

private void button1_Click(object sender, EventArgs e)
{
    IniFile iniFile = new IniFile();
    IniSection iniSection = null;

    foreach (DataGridViewRow row in this.dataGridView1.Rows)
    {
        if (row.IsNewRow)
            break;

        string section = row.Cells[0].Value?.ToString();
        string key = row.Cells[1].Value.ToString();
        string value = row.Cells[2].Value.ToString();

        if (!string.IsNullOrEmpty(section))
            iniSection = iniFile.Sections.Add(section);

        iniSection.Keys.Add(key, value);
    }

    iniFile.Save("C:\\Test.ini");
}

Кроме того, вот как выглядит сгенерированный файл "Test.ini":

[a]
123=456
789=234
345=678
[b]
123=456
789=234
345=678
...