0

In my console app I get the list of all installed programs which then displays in the console. But how can I save that to a text file?

This is my code I am using

private static void GetInstalledApps32()
    {
        string uninstallKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
        using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(uninstallKey))
        {
            foreach (string skName in rk.GetSubKeyNames())
            {
                using (RegistryKey sk = rk.OpenSubKey(skName))
                {
                    try
                    {
                        Console.WriteLine(sk.GetValue("DisplayName"));
                    }
                    catch (Exception ex)
                    { 
                    
                    }
                }
            }
        }
    }

I have searched for solutions but it then only writes one line instead of all the lines.

Thanks

  • Does this answer your question? [Easiest way to read from and write to files](https://stackoverflow.com/questions/7569904/easiest-way-to-read-from-and-write-to-files) – MindSwipe Oct 09 '20 at 10:47
  • `yourExecutable > output.txt` on cmd line? – Fildor Oct 09 '20 at 10:49

3 Answers3

0

Look at using FileStream and StreamWriter to write to the file specified in "OutputFilePath" instead of using the Console.

    private static void GetInstalledApps32()
    {
    string uninstallKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
    using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(uninstallKey))
    {
        using (FileStream fs = new FileStream("OutputFilePath", FileMode.Create))
        using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
        {
            foreach (string skName in rk.GetSubKeyNames())
            {
                using (RegistryKey sk = rk.OpenSubKey(skName))
                {
                    try
                    {
                        w.WriteLine(sk.GetValue("DisplayName"));
                    }
                    catch (Exception ex)
                    { 
                    
                    }
                }
            }
        }
    }
}

https://learn.microsoft.com/en-us/dotnet/api/system.io.filestream?view=netcore-3.1

Tim
  • 623
  • 5
  • 12
0

Do something like this it will give result in different lines

 private static void GetInstalledApps32()
    {
        string uninstallKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
        using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(uninstallKey))
        {
            foreach (string skName in rk.GetSubKeyNames())
            {
                using (RegistryKey sk = rk.OpenSubKey(skName))
                {
                    try
                    {
                        using (System.IO.StreamWriter file =
                        new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt", true))
                        {
                            file.WriteLine(sk.GetValue("DisplayName") + Environment.NewLine);
                        }
                    }
                    catch (Exception ex)
                    {

                    }
                }
            }
        }
    }
0

You already have a program that writes to StdOut, so you could simply do the following in Windows Command Line:

C:\>YourExecutable.exe > output.txtEnter

to redirect the output to that file.

If however you want to do it programmatically, I'd recommend looking at the duplicate linked by @MindSwipe.

Fildor
  • 14,510
  • 4
  • 35
  • 67