C# 在Windows CE C中登录时出错#

C# 在Windows CE C中登录时出错#,c#,windows-ce,C#,Windows Ce,我正在编写一个Windows CE应用程序,希望将错误记录到.txt文件 我做过研究,看到了这个例子: public static void Logs(string fileName, string methodName, string message) { try { if (!string.IsNullOrEmpty(message)) { using (FileStrea

我正在编写一个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);
}

并使用此函数代替
应用程序。StartupPath

@Werner从语句中删除
=
。好的,这样我在调试时不会出现任何错误,但是Log.txt不会像在Windows窗体中那样创建。您确定要将日志写入的路径正确吗?“Logs”函数可能会抛出一个异常,但您在捕获它时不会显示任何错误。尝试调试代码,看看是否达到catch块。debuged and Logs函数不会出现任何异常。不过,我对这条路不太清楚。之前的代码(我认为)是在应用程序的开始路径中写入log.txt,因此出现了
Application.StartupPath
。我说得对吗?我不认为我需要添加路径?我认为
GetCurrentApplicationPath
可以做到这一点?GetCurrentApplicationPath返回例如\MyCompany\MyProduct,因此您必须添加文件名。