본문 바로가기

Programing (프로그래밍)/WPF & C# (C Sharp)

WPF & C# - ini 쓰기, 다양하게 읽기 ( config / setup / Dictionary )

728x90
반응형


 WPF & C# - ini 쓰기, 다양하게 읽기 ( config / setup / Dictionary )



test_iniFile.zip





MainWindow.xaml.cs


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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// INI 쓰기
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string IpAppName, string key, string val, string filePath);
 
// INI 읽기 ( StringBuilder )
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string IpAppName, string key, string def, StringBuilder retVal, int size, string filePath);
 
// INI ( Integer )
[DllImport("kernel32")]
private static extern uint GetPrivateProfileInt(string lpAppName, string lpKeyName, int nDefault, string lpFileName);
 
// INI 읽기 (섹션[])
[DllImport("kernel32.dll")]
private static extern uint GetPrivateProfileSectionNames(byte[] IpSections, uint nSize, string IpFileName);
private string[] GetIniSectionNames()
{
    byte[] ba = new byte[255];
    uint Flag = GetPrivateProfileSectionNames(ba, 255, iniFile);
    return Encoding.Default.GetString(ba).Split(new char[1] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
}
 
// INI 읽기(섹션&KEY [])
[DllImport("kernel32.dll")]
private static extern uint GetPrivateProfileSection(string IpAppName, byte[] IpPairValues, uint nSize, string IpFileName);
private string[] GetIniValues(string Section)
{
    byte[] ba = new byte[255];
    uint Flag = GetPrivateProfileSection(Section, ba, 255, iniFile);
    return Encoding.Default.GetString(ba).Split(new char[1] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
}
 
 
 
// INI 위치
string iniFile = Environment.CurrentDirectory + @"\config.ini";
 
private void btnWrite_Click(object sender, RoutedEventArgs e)
{
    WritePrivateProfileString(tbSection01.Text, tbKey01.Text, tbVal01.Text, iniFile);
    WritePrivateProfileString(tbSection02.Text, tbKey02.Text, tbVal02.Text, iniFile);
    WritePrivateProfileString(tbSection02.Text, tbKey03.Text, tbVal03.Text, iniFile);
}
 
 
private void btnRead_Click(object sender, RoutedEventArgs e)
{
    // GetPrivateProfileString 읽기
    StringBuilder sb = new StringBuilder(255);
    GetPrivateProfileString(tbSection01.Text, tbKey01.Text, "", sb, sb.Capacity, iniFile);
    lbl01.Content = "[Key값 읽기]\n";
    lbl01.Content += "섹션 : " + tbSection01.Text + "\n";
    lbl01.Content += "키 : " + tbKey01.Text + "\n";
    lbl01.Content += "------------------\n";
    lbl01.Content += sb.ToString();
 
    // INI 읽기 (섹션[])
    string[] iniSectionNames = GetIniSectionNames();
    lbl02.Content = "섹션[] 읽기\n";
    lbl02.Content += "------------------\n";
    foreach (string a in iniSectionNames)
    {
        lbl02.Content += a + "\n";
    }
 
    // INI 읽기(Key&Value [])
    string[] iniGet = GetIniValues(tbSection02.Text);
    lbl03.Content = "Key&Value [] 읽기\n";
    lbl03.Content += "섹션 : " + tbSection02.Text + "\n";
    lbl03.Content += "------------------\n";
 
 
    Dictionary<stringstring> dic = new Dictionary<stringstring>();
    foreach (string str in iniGet)
    {
// 변경 - 20181027
        dic.Add(
            str.Substring(0, str.IndexOf("=")), // key
            str.Substring(str.IndexOf("=") + 1) // value
            );
    }
 
    string js = string.Empty;
    if (dic.TryGetValue("Key02"out js))
    {
        MessageBox.Show(js);
    }
 
    foreach (KeyValuePair<stringstring> keyVal in dic)
    {
        lbl03.Content += keyVal.Key + "/" + keyVal.Value + "\n";
    }
}
cs




MainWindow.xaml


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
<Window x:Class="test_iniFile2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:test_iniFile2"
        mc:Ignorable="d"
        Title="MainWindow" Height="383.661" Width="791.155">
    <Grid>
        <Button x:Name="btnWrite" Content="Write" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Click="btnWrite_Click"/>
        <TextBox x:Name="tbVal01" HorizontalAlignment="Left" Height="27" Margin="557,10,0,0" TextWrapping="Wrap" Text="Val01" VerticalAlignment="Top" Width="211" AcceptsReturn="True"/>
        <TextBox x:Name="tbVal02" HorizontalAlignment="Left" Height="27" Margin="557,53,0,0" TextWrapping="Wrap" Text="Val02" VerticalAlignment="Top" Width="211" AcceptsReturn="True"/>
        <TextBox x:Name="tbVal03" HorizontalAlignment="Left" Height="27" Margin="557,85,0,0" TextWrapping="Wrap" Text="Val03" VerticalAlignment="Top" Width="211" AcceptsReturn="True"/>
        <Label x:Name="lbl01" Content="" HorizontalAlignment="Left" Margin="102,139,0,0" VerticalAlignment="Top" Height="203" Width="211"/>
        <Button x:Name="btnRead" Content="Read" HorizontalAlignment="Left" Margin="10,139,0,0" VerticalAlignment="Top" Width="75" Click="btnRead_Click"/>
        <Label x:Name="lbl02" Content="" HorizontalAlignment="Left" Margin="330,139,0,0" VerticalAlignment="Top" Height="203" Width="211"/>
        <Label x:Name="lbl03" Content="" HorizontalAlignment="Left" Margin="557,139,0,0" VerticalAlignment="Top" Height="203" Width="211"/>
        <TextBox x:Name="tbSection01" HorizontalAlignment="Left" Height="27" Margin="102,10,0,0" TextWrapping="Wrap" Text="Section01" VerticalAlignment="Top" Width="211" AcceptsReturn="True"/>
        <TextBox x:Name="tbSection02" HorizontalAlignment="Left" Height="59" Margin="102,53,0,0" TextWrapping="Wrap" Text="Section02" VerticalAlignment="Top" Width="211" AcceptsReturn="True"/>
        <TextBox x:Name="tbKey01" HorizontalAlignment="Left" Height="27" Margin="330,10,0,0" TextWrapping="Wrap" Text="Key01" VerticalAlignment="Top" Width="211" AcceptsReturn="True"/>
        <TextBox x:Name="tbKey02" HorizontalAlignment="Left" Height="27" Margin="330,53,0,0" TextWrapping="Wrap" Text="Key02" VerticalAlignment="Top" Width="211" AcceptsReturn="True"/>
        <TextBox x:Name="tbKey03" HorizontalAlignment="Left" Height="27" Margin="330,85,0,0" TextWrapping="Wrap" Text="Key03" VerticalAlignment="Top" Width="211" AcceptsReturn="True"/>
    </Grid>
</Window>
 
cs



728x90
반응형