0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

めも

Last updated at Posted at 2019-09-09

■セクション削除
WritePrivateProfileString("section", NULL, NULL, "Test.ini");

■キー値削除
WritePrivateProfileString("section", "key", NULL, ".\TestIni.ini");

■UniCodeによるiniファイルの読み書き
[DllImport("Kernel32.dll", CharSet=CharSet.Unicode, EntryPoint="GetPrivateProfileStringW")]
public static string GetIniStringW(string section, string key, string defaultText, string fileName)
{
StringBuilder sb = new StringBuilder(300);
uint ret = GetPrivateProfileStringW(section, key, defaultText, sb, 300U, fileName);
return sb.ToString();
}

[DllImport("kernel32.dll", EntryPoint = "WritePrivateProfileStringW", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool WritePrivateProfileString(
string lpAppName,
string lpKeyName,
string lpString,
string lpFileName
);

■テキストファイルをリストに読み込む
string line = "";
ArrayList al = new ArrayList();

using (StreamReader sr = new StreamReader(
    "readme.txt", Encoding.GetEncoding(utf16.EncodingName))) {

  while ((line = sr.ReadLine()) != null) {
    al.Add(line);

■テキストファイルへの書き込み
(書き加えるときは、System.IOのAppendを使用)
var fileName = @"c:\test.txt";
var encoding = System.Text.Encoding.GetEncoding("SHIFT_JIS");

using (var writer = new System.IO.StreamWriter(fileName, false, encoding))
{
for (int i = 0; i < 10; ++i)
{
var record = string.Format("{0}", i);
writer.WriteLine(record);
}
}

//先頭文字の取得
string s = "あいうえお";
//先頭から2文字を取得する。先頭の文字のインデックスは0。
string s1 = s.Substring(0, 2);
//1文字目の文字を取得する
char c1 = str[0];

//Unicodeテキストファイルの書き出し
StreamWriter sw = new StreamWriter(@"D:\test1.txt", false, new UTF16Encoding(true));
sw.WriteLine("東京都新宿区");
sw.Close();

//文字コード(UniCode)
var utf16 = System.Text.Encoding.Unicode;

//ファイルの末尾にTextBox1の内容を書き加える
System.IO.File.AppendAllText(filePath, TextBox1.Text, utf16.EncodingName);

//"C:\test\1.txt"をUniCodeとして開く
System.IO.StreamReader sr = new System.IO.StreamReader(
@"C:\test\1.txt",
utf16.EncodingName);
//内容をすべて読み込む
string s = sr.ReadToEnd();
//閉じる
sr.Close();

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?