【问题标题】:Create a Log for WPF Application on client machine?在客户端计算机上为 WPF 应用程序创建日志?
【发布时间】:2012-01-04 01:49:04
【问题描述】:

当 WPF 应用程序部署为 MSI 安装程序并在客户端计算机上运行时,是否可以在 WPF 应用程序中创建文本文件并记录一些信息。

我尝试使用 File.create 等创建文件。但该文件未在客户端计算机上创建。

提前致谢。

                        using (FileStream stream = File.Create(@"c:\Log.txt"))
                        {
                            using (StreamWriter writer = new StreamWriter(stream))
                            {
                                writer.WriteLine("I can write to local disk.");
                            }
                        }

【问题讨论】:

  • 是的——这是可能的。您应该完善您的问题,并可能显示您正在使用的代码。
  • 我怀疑这是一种特权。最好登录到任何用户也可以写的地方。
  • @kenny 是否有任何用户可以创建和写入文件的位置?
  • 根据您的需要,其中一个特殊文件夹可能效果最好stackoverflow.com/questions/867485/…

标签: .net wpf c#-4.0


【解决方案1】:

一个选项是 .NET 中的跟踪 API。您可以通过配置文件对其进行配置以转到文件(文件侦听器)。默认转到调试输出,您可以使用 Sysinternals 的 DebugView。您甚至可以创建自定义跟踪侦听器。

此链接有一些概述链接:
https://docs.microsoft.com/en-us/archive/blogs/kcwalina/tracing-apis-in-net-framework-2-0

【讨论】:

    【解决方案2】:

    您应该能够在用户的本地应用程序数据存储中使用特定于应用程序的目录:

            string sDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "MyApplicationDir");
    
            if (!Directory.Exists(sDirectory))
            {
                Directory.CreateDirectory(sDirectory);
            }
    
            using (FileStream stream = File.Create(Path.Combine(sDirectory, "Log.txt"))
            {
                using (StreamWriter writer = new StreamWriter(stream))
                {
                    writer.WriteLine("I can write to local disk.");
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多