110,536
社区成员
发帖
与我相关
我的任务
分享
if (File.Exists("..\\..\\temp.dat"))
File.Delete("..\\..\\temp.dat");
File.Copy(openFileDialog1.FileName,"..\\..\\temp.dat");
IniFile file = new IniFile("..\\..\\temp.dat");
string[] section = file.GetSectionNames();
tabControl1.TabPages.Clear();
treeView1.Nodes.Clear();
TreeNode node = new TreeNode(Path.GetFileName(openFileDialog1.FileName));
treeView1.Nodes.Add(node);
treeView1.SelectedNode = node;
for (int j = 1; j < section.Count(); j++)
{
TabPage page1 = new TabPage(section[j]);
tabControl1.TabPages.Insert(j - 1, page1);
page1.Name = section[j];
page1.BackColor = Color.White;
page1.AutoScroll = true;
treeView1.SelectedNode.Nodes.Add(section[j].ToString());
string[] key = file.GetKeyNames(section[j]);
for (int i = 0; i < key.Count(); i++)
{
Label l = new Label();
l.Parent = page1;
l.Left = 20;
l.AutoSize = true;
l.Top = 20 + i * 20;
l.Name = i.ToString();
l.Text = key[i] + " :";
string value = file.GetString(section[j], key[i], "");
TextBox t = new TextBox();
t.Parent = page1;
t.Left = 320;
t.AutoSize = true;
t.Top = 20 + i * 20;
t.Name = key[i];
t.Text = value;
t.Width = 200;
t.BorderStyle = BorderStyle.Fixed3D;
t.TextChanged += new EventHandler(t_TextChanged);
}
}
treeView1.ExpandAll();
treeView1.SelectedNode = null;
[DllImport("kernel32")]
private static extern uint GetPrivateProfileString(
string lpAppName, // points to section name
string lpKeyName, // points to key name
string lpDefault, // points to default string
byte[] lpReturnedString, // points to destination buffer
uint nSize, // size of destination buffer
string lpFileName // points to initialization filename
);
/// <summary>
/// 读取section
/// </summary>
/// <param name="Strings"></param>
/// <returns></returns>
public List<string> ReadSections(string iniFilename)
{
List<string> result = new List<string>();
byte[] buf = new byte[65536];
uint len = GetPrivateProfileString(null, null, null, buf, (uint)buf.Length, iniFilename);
int k = 0;
for (int i = 0; i < len; i++)
if (buf[i] == 0)
{
result.Add(Encoding.Default.GetString(buf, k, i - k));
k = i + 1;
}
return result;
}
/// <summary>
/// 读取指定区域Keys列表。
/// </summary>
/// <param name="Section"></param>
/// <param name="Strings"></param>
/// <returns></returns>
public List<string> ReadSingleSection(string Section, string iniFilename)
{
List<string> result = new List<string>();
byte[] buf = new byte[65536];
uint lenf = GetPrivateProfileString(Section, null, null, buf, (uint)buf.Length, iniFilename);
int j = 0;
for (int i = 0; i < lenf; i++)
if (buf[i] == 0)
{
result.Add(Encoding.Default.GetString(buf, j, i - j));
j = i + 1;
}
return result;
}
/// <summary>
/// 010525 by skep99
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
string iniFilename = @"C:\a.ini";
List<string> sectionList = ReadSections(iniFilename);
foreach (string section in sectionList)
{
List<string> keyList = ReadSingleSection(section, iniFilename);
Console.WriteLine("Section[{0}]中Keys的数量为[{1}]", section, keyList.Count);
}
}
/// <summary>
/// 按section取key的值
/// </summary>
/// <param name="Section"></param>
/// <param name="iniFilename"></param>
/// <returns></returns>
public string ReadString(string Section, string key, string iniFilename)
{
byte[] buf = new byte[2048];
uint cnt = GetPrivateProfileString(Section, key, null, buf, (uint)buf.Length, iniFilename);
return Encoding.Default.GetString(buf, 0, (int)cnt);
}
if (File.Exists("..\\..\\temp.dat"))
File.Delete("..\\..\\temp.dat");
File.Copy(openFileDialog1.FileName, "..\\..\\temp.dat");
IniFile file = new IniFile("..\\..\\temp.dat");
IniFile file2 = new IniFile(openFileDialog1.FileName + "t");
string[] section = file2.GetSectionNames();
tabControl1.TabPages.Clear();
treeView1.Nodes.Clear();
TreeNode node = new TreeNode(Path.GetFileName(openFileDialog1.FileName));
treeView1.Nodes.Add(node);
treeView1.SelectedNode = node;
for (int j = 1; j < section.Count(); j++)
{
TabPage page1 = new TabPage(section[j]);
tabControl1.TabPages.Insert(j - 1, page1);
page1.Name = section[j];
page1.BackColor = Color.White;
page1.AutoScroll = true;
treeView1.SelectedNode.Nodes.Add(section[j].ToString());
string[] key = file2.GetKeyNames(section[j]);
for (int i = 0; i < key.Count(); i++)
{
Label l = new Label();
l.Parent = page1;
l.Left = 20;
l.AutoSize = true;
l.Top = 20 + i * 20;
l.Name = i.ToString();
l.Text = key[i] + " :";
//这里开始是动态生成textbox,我这个代码是错的
for (int a = 0; a < 1; a = a + key.Count())
{
TextBox t = new TextBox();
t.Parent = page1;
t.Left = 320;
t.AutoSize = true;
t.Top = 20 + i * 20;
//t.Text =
//t.Name =
t.Width = 200;
t.BorderStyle = BorderStyle.Fixed3D;
t.TextChanged += new EventHandler(t_TextChanged);
}
}
}
treeView1.ExpandAll();
treeView1.SelectedNode = null;
[DllImport("kernel32")]
private static extern uint GetPrivateProfileString(
string lpAppName, // points to section name
string lpKeyName, // points to key name
string lpDefault, // points to default string
byte[] lpReturnedString, // points to destination buffer
uint nSize, // size of destination buffer
string lpFileName // points to initialization filename
);
/// <summary>
/// 读取section
/// </summary>
/// <param name="Strings"></param>
/// <returns></returns>
public List<string> ReadSections(string iniFilename)
{
List<string> result = new List<string>();
byte[] buf = new byte[65536];
uint len = GetPrivateProfileString(null, null, null, buf,(uint)buf.Length, iniFilename);
int k = 0;
for (int i = 0; i < len; i++)
if (buf[i] == 0)
{
result.Add(Encoding.Default.GetString(buf, k, i - k));
k = i + 1;
}
return result;
}
/// <summary>
/// 010525 by skep99
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
ReadSections(@"C:\a.ini");
}