Programming/C#

[C#] .ini File Read & Write

BadaGreen_Kim 2019. 1. 15. 15:32

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;   //class 추가
 
namespace Login
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }
 
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal,
                                                        int size, string filePath);
         
 
        private void btnLogin_Click(object sender, EventArgs e)
        {
//ini 쓰기
            WritePrivateProfileString("LOGIN""ID""test""C:\\login.ini");
            WritePrivateProfileString("LOGIN""PW""1234""C:\\login.ini");
 
            StringBuilder retId = new StringBuilder();
            StringBuilder retPw = new StringBuilder();
 
//ini 읽기
            GetPrivateProfileString("LOGIN""ID""(NONE)", retId, 32"C:\\login.ini");
            GetPrivateProfileString("LOGIN""PW""(NONE)", retPw, 32"C:\\login.ini");
 
            MessageBox.Show("ID :"+ retId.ToString()+",PW: "+ retPw.ToString());
 
cs