Windows CEアプリを作成していて、.txtファイルにエラーログを実装したいと考えています。
私は調査を行い、この例を見ました:
public static void Logs(string fileName, string methodName, string message)
{
try
{
if (!string.IsNullOrEmpty(message))
{
using (FileStream file = new FileStream(Application.StartupPath + "\\Log.txt", FileMode.OpenOrCreate, FileAccess.Write))
{
StreamWriter streamWriter = new StreamWriter(file);
streamWriter.WriteLine((((System.DateTime.Now + " - ") + fileName + " - ") + methodName + " - ") + message);
streamWriter.Close();
}
}
}
catch
{
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
int a = 10, b = 0;
int result = a / b; //runtime Exception will throw
}
catch (Exception ex)
{
Logs(this.GetType().Name, "button1_Click()", ex.Message.ToString());
}
}
これは機能しますが、Windowsフォームでのみ機能します。WindowsCEで次のエラーが発生します。
'System.Windows.Application' does not contain a definition for 'StartupPath'
わかりました、それで私はこれを使わなければならないことを知っています:
Path.GetDirectoryName (Assembly.GetExecutingAssembly ().GetName ().CodeBase);
しかし、その方法はわかりません。まだ学習中なので、これを重複としてマークしないでください。誰かがWindowsCEアプリでこれをどこでどのように使用するかを正確に説明してもらえますか?
前もって感謝します。