ファイルの作成、更新、アクセス日時の取得&設定 [C#]
ファイルの作成日時、更新日時、アクセス日時の取得と設定をする
System.IO.Fileクラスでそれぞれ取得、設定できる。
ファイルではなくディレクトリの場合はSystem.IO.FileクラスをSystem.IO.Direcroryに変える。
string strFile = Server.MapPath("aaa.txt");
DateTime dtFileCreateTime;
DateTime dtFileUpdateTime;
DateTime dtFileAccessTime;
//作成日時の取得
dtFileCreateTime = System.IO.File.GetCreationTime(strFile);
//更新日時の取得
dtFileUpdateTime = System.IO.File.GetLastWriteTime(strFile);
//アクセス日時の取得
dtFileAccessTime = System.IO.File.GetLastAccessTime(strFile);
//全て一日前に戻す
//作成日時の設定
System.IO.File.SetCreationTime(strFile, dtFileCreateTime.AddDays(-1));
//更新日時の設定
System.IO.File.SetLastWriteTime(strFile, dtFileCreateTime.AddDays(-1));
//アクセス日時の設定
System.IO.File.SetLastAccessTime(strFile, dtFileCreateTime.AddDays(-1));
うるう年の判定をする [C#]
うるう年の判定は、4で割り切れたらうるう年だけど、
100で割り切れる場合はうるう年じゃなくて、400で割り切れる場合はうるう年
(1900年はうるう年じゃないが、2000年はうるう年)の用にちょっと面倒だが、
DateTime.IsLeapYearを使えば一発で判定できて楽です。
int intYear = Convert.ToInt32(tbx_year.Text);
bool blnLeapYear = DateTime.IsLeapYear(intYear);
if (blnLeapYear)
{
Response.Write("うるう年です。");
}
else
{
Response.Write("うるう年ではありません。");
}
WebRequestとWebResponseでWeb情報を取得 [C#]
Windowsアプリケーション、またはASP.NETによるWebアプリケーションから、
WebRequestとWebResponseを使用してデータを取得する。
string strUrl = "http:⁄⁄localhost/request.aspx";
//エンコードの指定
System.Text.Encoding enc = System.Text.Encoding.GetEncoding("Shift_JIS");
//WebRequestの作成
System.Net.WebRequest wReq = System.Net.WebRequest.Create(strUrl );
//応答を受信するWebResponse
System.Net.WebResponse wRes = wReq.GetResponse();
//取得したレスポンスのストリームを取得
System.IO.Stream st = wRes.GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(st, enc);
string strResult = sr.ReadToEnd();
sr.Close();
st.Close();
Response.Write(strResult);
テキストファイルに文字列を追加する [C#]
関連記事:テキストファイルの入出力
テキストファイルの末尾に文字列を追加する方法。
このSystem.IO.File.AppendAllTextも.NET Framework2.0からの機能。
string strFile = "ファイル名";
//string strFile = Server.MapPath("ファイル名");
//末尾に追加するテキスト
string strAppendText = "こんばんは。";
//Shift_JISのエンコード指定
System.Text.Encoding enc = System.Text.Encoding.GetEncoding("Shift_JIS");
System.IO.File.AppendAllText(strFile , strAppendText , enc);
テキストファイルの入出力 [C#]
.NET Framework2.0からテキストファイルの入出力が以前比べて簡単になったので、
その方法をメモ。
string strFile = "ファイル名";
//string strFile = Server.MapPath("ファイル名");
//Shift_JISのファイルを読み込む。
System.Text.Encoding enc = System.Text.Encoding.GetEncoding("Shift_JIS");
//テキストファイルの中身をすべて読み込む
string strText = System.IO.File.ReadAllText(strFile, enc);
//テキストファイルを配列として行ごとに読み込む
string[] strTextLines = System.IO.File.ReadAllLines(strFile, enc);
string strFile = "ファイル名";
//string strFile = Server.MapPath("ファイル名");
//入力するテキスト
string strInputText = "こんにちは";
//Shift_JISのファイルに書き込む。
System.Text.Encoding enc = System.Text.Encoding.GetEncoding("Shift_JIS");
System.IO.File.WriteAllText(strFile , strInputText , enc);
乱数を作成する [C#]
C#で乱数を作成する方法。
System.Random rand = new System.Random();
//0と1の間の乱数を浮動小数点で作成
double dblRdm = rand.NextDouble();
//0以上の乱数を作成
int intRdm1 = rand.Next();
//0から設定した値までの乱数を作成
int intRdm2 = rand.Next(100);
//設定した値Aから設定した値Bまでの乱数を作成
int intRdm3 = rand.Next(50,100);
ディレクトリ内のファイルを検索 [C#]
指定したフォルダ内のファイルを拡張子やファイル名で検索して抽出する。
その際に指定フォルダ内の全てのサブフォルダ内のファイルも検索する方法。
//string strDir = Server.MapPath("検索対象のフォルダパス");
//拡張子がjpgのファイルのみ抽出。*はワイルドカード
//System.IO.SearchOption.AllDirectoriesをパラメーターに含めると、指定フォルダ以下の
//サブフォルダ内も全て検索する。
string[] strFiles = System.IO.Directory.GetFiles
(strDir, "*.jpg",System.IO.SearchOption.AllDirectories);
//検索されたファイル数
int intLen = strFiles.Length;
画像の縦横サイズを調べて、サイズを変えて保存する [C#]
ファイル上にある画像を読み込んで、画像のwidth、heightを調べて、
更にサイズを変更して、別ファイルに保存する。
string strPic = "image.jpg";
//string strPic = Server.MapPath("image.jpg");
//保存先ファイル
string strNewPic = "newimage.jpg";
//string strNewPic = Server.MapPath("newimage.jpg");
System.Drawing.Bitmap bmpSrc = new System.Drawing.Bitmap(strPic);
//元画像の縦横サイズを調べる
int width = bmpSrc.Width;
int height = bmpSrc.Height;
//元画像の縦横サイズを半分にする
System.Drawing.Bitmap bmpSrcHalf =
new System.Drawing.Bitmap(bmpSrc , width/2 , height/2 );
//フォーマットをJPGに指定して名前を変えて保存、
//デフォルトでSaveを実行するとPNGフォーマットになってしまう。
bmpSrcHalf.Save(strNewPic, System.Drawing.Imaging.ImageFormat.Jpeg);
メールを7bitでエンコードして送信する [C#]
.NET Framework2.0 よりメールの送信にはSystem.Net.Mail名前空間を使用する事が推奨されている。
System.Web.Mailを使う事もできるが、[使用しないでください。]と言われてしまう・・・。
System.Net.Mailでメールを送信すると、
通常Content-Transfer-Encodingがquoted-printableで送信されてしまう。
この場合、一部のメーラーやWebメールサービスでは文字化けが起きてしまう。
それを回避し、7bit(sevenbit)エンコードでメールを送信する方法。
string strFromName = "送信者の名前表示";
string strToMail = "受信者のメールアドレス";
string strToName = "受信者の名前表示";
string strSubject = "タイトル";
string strBodyText = "メール本文";
string strSubjectFormat = "=?{0}?B?{1}?=";
string strMailServerHost = "localhost" //メールサーバー
//JISコード
System.Text.Encoding enc = System.Text.Encoding.GetEncoding(50220);
//MailMessageの作成
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.From = new System.Net.Mail.MailAddress
(strFromMail, EncodeMailHeader(strFromName , enc));
msg.To.Add(new System.Net.Mail.MailAddress
(strToMail, EncodeMailHeader(strToName , enc));
//メールタイトルをバイト配列化
byte[] byteSubject = enc.GetBytes(strSubject);
//メールタイトルを変換
string strSubjectText = String.Format(strSubjectFormat, "ISO-2022-JP",
Convert.ToBase64String(byteSubject, Base64FormattingOptions.None));
//メールタイトルを追加
msg.Subject = strSubjectText;
//プレーンテキストのAlternateViewを作成
System.Net.Mail.AlternateView htmlView =
System.Net.Mail.AlternateView.CreateAlternateViewFromString
(strBodyText,enc,System.Net.Mime.MediaTypeNames.Text.Plain);
//TransferEncoding.SevenBitを指定
htmlView.TransferEncoding = System.Net.Mime.TransferEncoding.SevenBit;
//AlternateViewを追加
msg.AlternateViews.Add(htmlView);
System.Net.Mail.SmtpClient sc = new System.Net.Mail.SmtpClient();
//SMTPサーバーを指定する
sc.Host = strMailServerHost;
//メッセージを送信する
sc.Send(msg);
msg.Dispose();