6月 12

ディレクトリ(フォルダ)の移動/ディレクトリ名(フォルダ名)の変更

System.IO.Directory.Move()関数を使います。
移動元フォルダ名と
移動先フォルダ名を指定します。


// フォルダを移動する(移動の際、フォルダ名を変更しても構いません)
//
// ※移動元フォルダが無い場合
//      例外:System.IO.DirectoryNotFoundException   が発生します。
// ※移動先フォルダが既に存在する場合
//      例外:System.IO.IOException             が発生します。
System.IO.Directory.Move( @"C:\unko", @"C:\Temp\unko" );

// 移動元と移動先のフォルダが同じであれば名前を変更したことになります。
System.IO.Directory.Move( @"C:\unko", @"C:\hoge" );

6月 12

ディレクトリ(フォルダ)を削除する

指定フォルダを削除します。
System.IO.Directory.Delete()関数を使います。


// 削除するフォルダが存在しない場合は
//      System.IO.DirectoryNotFoundException 例外が発生します。
// フォルダが空でない場合は
//      System.IO.IOException 例外が発生します。
System.IO.Directory.Delete(@"c:\unko");

// フォルダ内にファイルやフォルダが存在しても
// 問答無用で削除したい場合は
// 第二パラメータにtrueを指定
System.IO.Directory.Delete(@"c:\unko",true);

6月 12

デバッグ情報をイベントログに出力する

デバッグ情報をイベントログに出力したい場合は
System.Diagnostics.Debug.Listenersコレクションに
EventLogTraceListenerを追加します。

イベントログは
【コントロールパネル】の【管理ツール】の【イベントビューア】で確認できます。
下のショットはVistaの場合なのでXPなどは若干画面が違うかもしれません。

debug_event

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 出力先としてイベントログを追加
            System.Diagnostics.EventLogTraceListener eventlog =
                new System.Diagnostics.EventLogTraceListener("C#サンプル");
            System.Diagnostics.Debug.Listeners.Add(eventlog);

            // 何か適当に出力
            for (int i = 1; i <= 10; i++)
                System.Diagnostics.Debug.WriteLine(i + "行目");

            // 出力バッファをフラッシュし、キャッシュをちゃんと書き込む
            System.Diagnostics.Debug.Flush();

            MessageBox.Show("イベントログ開いてみてください");
        }
    }
}
6月 12

デバッグ情報をファイルに出力する

デバッグ情報をファイルに出力したい場合は
System.Diagnostics.Debug.Listenersコレクションに
出力先として追加したいファイルを追加します。

出力先の出力パネルが表示されていない場合は
VisualStudioのメニューより
【表示】→【出力】
を選択して表示してください。

debug_file

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // デバッグ情報を出力するファイル名(実行ファイルと同じフォルダに置きます)
            string filename = AppDomain.CurrentDomain.BaseDirectory + "debug.txt";

            // 出力先としてテキストファイルを追加
            System.Diagnostics.TextWriterTraceListener texttrace = 
                new System.Diagnostics.TextWriterTraceListener(filename);
            System.Diagnostics.Debug.Listeners.Add(texttrace);

            // 何か適当に出力
            for (int i = 1; i <= 10; i++)
                System.Diagnostics.Debug.WriteLine(i + "行目");

            // 出力バッファをフラッシュし、キャッシュをちゃんと書き込む
            System.Diagnostics.Debug.Flush();

            // debug.txtファイルをメモ帳などで開いてみてください
            MessageBox.Show("debug.txtファイルをメモ帳などで開いてみてください");
        }
    }
}
6月 12

デバッグ情報を条件を指定して出力する

デバッグ情報を条件を指定して出力したい場合は
System.Diagnostics.DebugクラスのWriteIf関数やWriteLineIf関数を利用します。

例えばWriteLineIf関数の引数は2つ。
第一パラメータで出力するか否か
第二パラメータでその内容

例ではfor文ループ中にカウンタが偶数の場合のみ
出力しています。

出力先の出力パネルが表示されていない場合は
VisualStudioのメニューより
【表示】→【出力】
を選択して表示してください。

debug_if

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 1; i <= 10; i++)
                System.Diagnostics.Debug.WriteLineIf(i % 2 == 0 ? true : false, i + "は偶数です");
        }
    }
}
6月 12

デバッグ情報をインデントして出力する

デバッグ情報をインデント(字下げ)して出力したい場合は
System.Diagnostics.DebugクラスのIndent関数を使います。

1回のインデントで何文字字下げされるかを
IndentSizeプロパティーで指定することも可能です(初期値は4)

出力先の出力パネルが表示されていない場合は
VisualStudioのメニューより
【表示】→【出力】
を選択して表示してください。

debug_indent

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("まずは普通に出力");

            // 1つインデントしてみます
            System.Diagnostics.Debug.Indent();

            System.Diagnostics.Debug.WriteLine("インデントされました");

            // さらにもう1つインデントしてみます
            System.Diagnostics.Debug.Indent();

            System.Diagnostics.Debug.WriteLine("さらにインデントされました");

            // 2回インデントされているので2回Unindentして元に戻します
            System.Diagnostics.Debug.Unindent();
            System.Diagnostics.Debug.Unindent();

            System.Diagnostics.Debug.WriteLine("元に戻りました");
        }
    }
}
6月 12

デバッグ情報を出力する

デバッグ情報を出力したい場合は
System.Diagnostics.DebugクラスのWriteやWriteLine関数を使います。

出力先の出力パネルが表示されていない場合は
VisualStudioのメニューより
【表示】→【出力】
を選択して表示してください。

debug

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 文字列を出力
            System.Diagnostics.Debug.Write("あいうえお\r\n");

            // 文字列を改行付きで出力
            System.Diagnostics.Debug.WriteLine("かきくけこ");
        }
    }
}
6月 12

曜日を取得する

DateTime構造体のDayOfWeekプロパティーを参照します。

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // 現在の日付と時刻を取得
            DateTime date = DateTime.Now;

            // 曜日を取得
            Console.WriteLine(date.DayOfWeek);

            // 日本語で
            string week = string.Empty;
            switch (date.DayOfWeek)
            {
                case DayOfWeek.Sunday: week = "日"; break;
                case DayOfWeek.Monday: week = "月"; break;
                case DayOfWeek.Tuesday: week = "火"; break;
                case DayOfWeek.Wednesday: week = "水"; break;
                case DayOfWeek.Thursday: week = "木"; break;
                case DayOfWeek.Friday: week = "金"; break;
                case DayOfWeek.Saturday: week = "土"; break;
            }

            Console.WriteLine(week + "曜日です。");
        }
    }
}

【結果】

Tuesday
火曜日です。
6月 12

任意の日付と時刻を設定/取得する

DateTime構造体に任意の日付と時刻を設定、或いは取得する方法です。

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // 年月日を設定(ここでは年月日が引数ですが続けて時分秒も指定可能です)
            DateTime date = new DateTime(2008, 8, 5);

            // 年月日を取得
            Console.WriteLine(date.Year + "年" + date.Month + "月" + date.Day + "日");

            // 時、分、秒、ミリ秒を設定
            // 以下のプロパティーは読み取り専用なのでコンパイルエラー
            //date.Hour = 10;
            //date.Minute = 30;
            //date.Second = 45;
            //date.Millisecond = 100;

            // 時、分、秒、などの値を変更したい場合はあらためてnewします
            date = new DateTime(date.Year, date.Month, date.Day, 10, 30, 45);

            // 時刻を出力
            Console.WriteLine(date.ToLongTimeString());
        }
    }
}

【結果】

2008年8月5日
10:30:45