專案路徑下新增專案內的
\bin\Debug
裡面新增設定檔 myConfig.ini
using System.Runtime.InteropServices;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
[DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern bool WritePrivateProfileString(string sectionName, string keyName, string keyValue, string filePath);
[DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern int GetPrivateProfileString(string sectionName, string keyName, string defaultReturnString, StringBuilder returnString, int returnStringLength, string filePath);
public Form1()
{
InitializeComponent();
//取得應用程式的可執行檔路徑,不包括檔名,通常都是位於Debug資料夾
string Rundir = Application.StartupPath + "\\myConfig.ini";
//建立ini檔案
WritePrivateProfileString("Owner", "name", "Dan Mac", @"D:\Temp\myConfig.ini");
WritePrivateProfileString("Owner", "organization", "Apple Inc.", @"D:\Temp\myConfig.ini");
WritePrivateProfileString("Database", "server", "192.168.1.100", @"D:\Temp\myConfig.ini");
WritePrivateProfileString("Database", "port", "50000", @"D:\Temp\myConfig.ini");
WritePrivateProfileString("Database", "file", "\"db.dat\"", @"D:\Temp\myConfig.ini");
//讀取ini檔案
StringBuilder data = new StringBuilder(255);
GetPrivateProfileString("Database", "file", "NA", data, 255, Rundir);
Console.WriteLine(data);
}
}
}
參考出處:
http://einboch.pixnet.net/blog/post/250128728
http://blog.yam.com/phone875/article/25773797
沒有留言:
張貼留言