コンボボックス cbo01 のデータソースが
this.cbo01.DataSource = this.Dataset01BindingSource;
のとき
System.Data.DataRowView view01 = (DataRowView)this.cbo01.SelectedItem;
と書くことで、view01にレコードが保存される
たとえば0番目のカラムの内容を表示したければ
MessageBox.Show(view01.Row.ItemArray[0].ToString());
2009年8月24日月曜日
2009年8月21日金曜日
2009年8月20日木曜日
C# 変数等のデータ型を変更する
[C#]
//文字列をint型に変換
int i = int.Parse("100");
//文字列をlong型に変換
long l = long.Parse("100");
//文字列をfloat型に変換
float f = float.Parse("100.1");
//文字列をdouble型に変換
double d = double.Parse("100.1");
//文字列をbool型に変換
bool b = bool.Parse("True");
//文字列をint型に変換
int i = int.Parse("100");
//文字列をlong型に変換
long l = long.Parse("100");
//文字列をfloat型に変換
float f = float.Parse("100.1");
//文字列をdouble型に変換
double d = double.Parse("100.1");
//文字列をbool型に変換
bool b = bool.Parse("True");
C#でアプリケーションを終了させる
C# でアプリケーションを終了させるには System.Environment.Exit() を使う。
Environment.Exit(0);
http://sonic64.com/2006-05-07.html
Environment.Exit(0);
http://sonic64.com/2006-05-07.html
2009年8月17日月曜日
C# pathからファイル名を得る
// getfilename.cs
using System;
using System.IO;
public class GetFileName {
static void Main() {
string path1 = @"c:\windows\system32\notepad.exe";
string file1 = Path.GetFileName(path1);
Console.WriteLine(file1); // 出力例:notepad.exe
string path2 = "c:notepad.exe";
string file2 = Path.GetFileName(path2);
Console.WriteLine(file2); // 出力例:notepad.exe
string path3 = "http://www.atmarkit.co.jp/index.html";
string file3 = Path.GetFileName(path3);
Console.WriteLine(file3); // 出力例:index.html
}
}
using System;
using System.IO;
public class GetFileName {
static void Main() {
string path1 = @"c:\windows\system32\notepad.exe";
string file1 = Path.GetFileName(path1);
Console.WriteLine(file1); // 出力例:notepad.exe
string path2 = "c:notepad.exe";
string file2 = Path.GetFileName(path2);
Console.WriteLine(file2); // 出力例:notepad.exe
string path3 = "http://www.atmarkit.co.jp/index.html";
string file3 = Path.GetFileName(path3);
Console.WriteLine(file3); // 出力例:index.html
}
}
C# 設定を使用する
// 設定する
ソリューションエクスプローラー > prorerties > 設定
で設定した変数を呼び出す
// 呼び出す
Properties.Settings.Default.hensu1
// 参照
http://msdn.microsoft.com/ja-jp/library/aa730869%28VS.80%29.aspx
ソリューションエクスプローラー > prorerties > 設定
で設定した変数を呼び出す
// 呼び出す
Properties.Settings.Default.hensu1
// 参照
http://msdn.microsoft.com/ja-jp/library/aa730869%28VS.80%29.aspx
C# ファイル関連
//C:\test.txtをC:\test1.txtに移動
//移動元と移動先が同じでも、例外はスローされない
//C:\test1.txtがすでにあるときは、例外がスローされる
System.IO.File.Move(@"C:\test.txt", @"C:\test1.txt");
//C:\test.txtをC:\test1.txtにコピー
//第3項にtrueを指定することにより、上書きを許可
System.IO.File.Copy(@"C:\test.txt", @"C:\test1.txt", true);
//C:\test.txtを削除
//指定したファイルが存在しなくても例外はスローされない
System.IO.File.Delete(@"C:\test.txt");
//属性の取得(FileAttributes列挙体を返す)
Console.WriteLine(System.IO.File.GetAttributes(@"C:\test.txt"));
//読み取り専用属性があるか調べる
if ((System.IO.File.GetAttributes(@"C:\test.txt") &
System.IO.FileAttributes.ReadOnly) ==
System.IO.FileAttributes.ReadOnly)
Console.WriteLine("読み取り専用属性があります。");
//作成日時の取得(DateTime値を返す)
Console.WriteLine(System.IO.File.GetCreationTime(@"C:\test.txt"));
//更新日時の取得(DateTime値を返す)
Console.WriteLine(System.IO.File.GetLastWriteTime(@"C:\test.txt"));
//アクセス日時の取得(DateTime値を返す)
Console.WriteLine(System.IO.File.GetLastAccessTime(@"C:\test.txt"));
//属性の設定
//読み取り専用属性を追加する
System.IO.File.SetAttributes(@"C:\test.txt",
System.IO.File.GetAttributes(@"C:\test.txt") |
System.IO.FileAttributes.ReadOnly);
//読み取り専用属性を削除する
System.IO.File.SetAttributes(@"C:\test.txt",
System.IO.File.GetAttributes(@"C:\test.txt") ^
System.IO.FileAttributes.ReadOnly);
//作成日時の設定(今の時間にする)
System.IO.File.SetCreationTime(@"C:\test.txt", DateTime.Now);
//更新日時の設定
System.IO.File.SetLastWriteTime(@"C:\test.txt", DateTime.Now);
//アクセス日時の設定
System.IO.File.SetLastAccessTime(@"C:\test.txt", DateTime.Now);
// C:\test.txtが存在するか調べる
if (!System.IO.File.Exists(@"C:\test.txt"))
{
Console.WriteLine("fileがないっす");
}
//移動元と移動先が同じでも、例外はスローされない
//C:\test1.txtがすでにあるときは、例外がスローされる
System.IO.File.Move(@"C:\test.txt", @"C:\test1.txt");
//C:\test.txtをC:\test1.txtにコピー
//第3項にtrueを指定することにより、上書きを許可
System.IO.File.Copy(@"C:\test.txt", @"C:\test1.txt", true);
//C:\test.txtを削除
//指定したファイルが存在しなくても例外はスローされない
System.IO.File.Delete(@"C:\test.txt");
//属性の取得(FileAttributes列挙体を返す)
Console.WriteLine(System.IO.File.GetAttributes(@"C:\test.txt"));
//読み取り専用属性があるか調べる
if ((System.IO.File.GetAttributes(@"C:\test.txt") &
System.IO.FileAttributes.ReadOnly) ==
System.IO.FileAttributes.ReadOnly)
Console.WriteLine("読み取り専用属性があります。");
//作成日時の取得(DateTime値を返す)
Console.WriteLine(System.IO.File.GetCreationTime(@"C:\test.txt"));
//更新日時の取得(DateTime値を返す)
Console.WriteLine(System.IO.File.GetLastWriteTime(@"C:\test.txt"));
//アクセス日時の取得(DateTime値を返す)
Console.WriteLine(System.IO.File.GetLastAccessTime(@"C:\test.txt"));
//属性の設定
//読み取り専用属性を追加する
System.IO.File.SetAttributes(@"C:\test.txt",
System.IO.File.GetAttributes(@"C:\test.txt") |
System.IO.FileAttributes.ReadOnly);
//読み取り専用属性を削除する
System.IO.File.SetAttributes(@"C:\test.txt",
System.IO.File.GetAttributes(@"C:\test.txt") ^
System.IO.FileAttributes.ReadOnly);
//作成日時の設定(今の時間にする)
System.IO.File.SetCreationTime(@"C:\test.txt", DateTime.Now);
//更新日時の設定
System.IO.File.SetLastWriteTime(@"C:\test.txt", DateTime.Now);
//アクセス日時の設定
System.IO.File.SetLastAccessTime(@"C:\test.txt", DateTime.Now);
// C:\test.txtが存在するか調べる
if (!System.IO.File.Exists(@"C:\test.txt"))
{
Console.WriteLine("fileがないっす");
}
登録:
投稿 (Atom)