在控制台应用程序中,我会获得所有已安装程序的列表,然后显示在控制台中。但是如何将其保存到文本文件中?
这是我的代码,我正在使用
private static void GetInstalledApps32()
{
string uninstallKey = @"SOFTWAREMicrosoftWindowsCurrentVersionUninstall";
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)
{
}
}
}
}
}
我一直在寻找解决方案,但它只写一行,而不是所有的行。
感谢
查看使用FileStream和StreamWriter写入";输出文件路径";而不是使用控制台。
private static void GetInstalledApps32()
{
string uninstallKey = @"SOFTWAREMicrosoftWindowsCurrentVersionUninstall";
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
您已经有了一个写入StdOut的程序,因此您可以在Windows命令行中简单地执行以下操作:
C:\>YourExecutable.exe>output.txt输入
将输出重定向到该文件。
然而,如果你想以编程方式进行,我建议你查看@MindSwipe链接的副本。
这样做会在不同的行中产生结果
private static void GetInstalledApps32()
{
string uninstallKey = @"SOFTWAREMicrosoftWindowsCurrentVersionUninstall";
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:UsersPublicTestFolderWriteLines2.txt", true))
{
file.WriteLine(sk.GetValue("DisplayName") + Environment.NewLine);
}
}
catch (Exception ex)
{
}
}
}
}
}