C# と VB.NET の質問掲示板

わんくま同盟

ASP.NET、C++/CLI、Java 何でもどうぞ

C# と VB.NET の入門サイト


(過去ログ 76 を表示中)
■44858 / )  Re[2]: c#でINIファイルの全セクションを取得
□投稿者/ たろ (55回)-(2009/12/22(Tue) 11:23:50)
ご返信ありがとうございます!


GetPrivateProfileStringByByteArray関数を使用して取得する事ができました!


[DllImport("KERNEL32.DLL", EntryPoint = "GetPrivateProfileStringA")]
public static extern uint
GetPrivateProfileStringByByteArray(string lpAppName,
string lpKeyName, string lpDefault,
byte[] lpReturnedString, uint nSize,
string lpFileName);


ArrayList aryRet = new ArrayList();
// 指定ファイルのセクションの一覧を得る
byte[] ar2 = new byte[1024];
uint resultSize2 = GetPrivateProfileStringByByteArray(null, null, "WEB01", ar2, (uint)ar2.Length, m_iniFilePath);

string result2 = Encoding.Default.GetString(ar2, 0, (int)resultSize2 - 1);
string[] sections = result2.Split('\0');
string val_tmp = "";
foreach (string section in sections)
{
}

なるほど!
GetPrivateProfileString関数でもできたんですね!
勉強になります!

ありがとございました!


No44848 (魔界の仮面弁士 さん) に返信
> ■No44846 (たろ さん) に返信
>>セクション名を指定して情報を取得する処理は、
>>[DllImport("kernel32")]
>>private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size,string filePath);
>>を使用して実現できているのですが、
>>セクション名だけを取得する処理がわかりません。。
>
> GetPrivateProfileString API だけでいけますよ。
> http://msdn.microsoft.com/ja-jp/library/cc429779.aspx
>
> // C#
> [DllImport("kernel32", CharSet = CharSet.Auto)]
> private static extern int GetPrivateProfileString(
> string section,
> string key,
> string def,
> char[] retVal,
> int size,
> string filePath);
>
> static void Main(string[] args)
> {
> char[] buf = new char[1024];
> int ret = GetPrivateProfileString(null, null, null, buf, buf.Length, "odbc.ini");
> string sectionsText = new string(buf, 0, ret);
> string[] sections = sectionsText.TrimEnd('\0').Split('\0');
>
> // 内容確認
> Array.ForEach(sections, Console.WriteLine);
> }
>
> '-----------------------------------------
>
> ' Visual Basic
> Declare Auto Function GetPrivateProfileString Lib "kernel32" _
> (ByVal section As String, _
> ByVal key As String, _
> ByVal def As String, _
> <Out(), MarshalAs(UnmanagedType.LPTStr)> ByVal buf As String, _
> ByVal size As Integer, _
> ByVal file As String) As Integer
>
> Sub Main()
> Dim buf As String = StrDup(1024, vbNullChar)
> Dim ret As Integer = GetPrivateProfileString(Nothing, Nothing, Nothing, buf, buf.Length, "odbc.ini")
> Dim sections() As String = Left(buf, ret).TrimEnd(vbNullChar).Split(vbNullChar)
>
> '内容確認
> Array.ForEach(sections, AddressOf Console.WriteLine)
> End Sub
返信 編集キー/


管理者用

- Child Tree -