繁体   English   中英

在Windows CE C#中记录错误

[英]Error logging in Windows CE C#

我正在编写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窗体。 我在Windows CE中收到此错误:

'System.Windows.Application' does not contain a definition for 'StartupPath'

好的,所以我知道我必须使用这个:

Path.GetDirectoryName (Assembly.GetExecutingAssembly ().GetName ().CodeBase);

但我不确定如何。 仍在学习,请不要将其标记为重复。 有人可以在Windows CE应用程序中向我详细解释在哪里以及如何使用它吗?

提前致谢。

在此输入图像描述

您可以编写一个函数来返回正在执行的程序集的路径...就像:

public static string GetCurrentApplicationPath() 
{
  return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
}

并使用此函数代替Application.StartupPath

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM