0

Is it possible to create a text file and log some info in WPF application when it is deployed as MSI installer and running on a client machine.

I tried creating a file using File.create etc. But the file is not getting created on client machine.

Thanks in advance.

                        using (FileStream stream = File.Create(@"c:\Log.txt"))
                        {
                            using (StreamWriter writer = new StreamWriter(stream))
                            {
                                writer.WriteLine("I can write to local disk.");
                            }
                        }
Sandeep
  • 7,156
  • 12
  • 45
  • 57
  • Yes - it is possible. You should refine your question, and perhaps show the code you are using. – Origin Jan 04 '12 at 01:51
  • I suspect it's a privilege thing. It's best to log to a place that any user can write too. – kenny Jan 04 '12 at 02:05
  • @kenny Is there any location where any user can create and write file? – Sandeep Jan 04 '12 at 02:11
  • 1
    One of the special folders will probably work best depending on your needs http://stackoverflow.com/questions/867485/c-sharp-getting-the-path-of-appdata – kenny Jan 04 '12 at 02:49

2 Answers2

2

One option is the tracing APIs in .NET. You can configure it via config files to go to a file (file listener). The default goes to debug output and you can use DebugView from Sysinternals. You can even create custom trace listeners.

This link has some overview links:
https://learn.microsoft.com/en-us/archive/blogs/kcwalina/tracing-apis-in-net-framework-2-0

Pang
  • 9,564
  • 146
  • 81
  • 122
bryanmac
  • 38,941
  • 11
  • 91
  • 99
1

You should be able to use and application-specific directory in the user's local application data store:

        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.");
            }
        }
competent_tech
  • 44,465
  • 11
  • 90
  • 113