I am trying to print the file attributes, for every file in a given folder, to a new text file.
I can print them to the console successfully with this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApp2
{
class Program
{
[STAThread]
public static void Main(string[] args)
{
List<string> arrHeaders = new List<string>();
Shell32.Shell shell = new Shell32.Shell();
Shell32.Folder objFolder;
objFolder = shell.NameSpace(@"C:\testfolder\testprop");
for (int i = 0; i < short.MaxValue; i++)
{
string header = objFolder.GetDetailsOf(null, i);
if (String.IsNullOrEmpty(header))
break;
arrHeaders.Add(header);
}
foreach (Shell32.FolderItem2 item in objFolder.Items())
{
for (int i = 0; i < arrHeaders.Count; i++)
{
Console.WriteLine(
$"{i}\t{arrHeaders[i]}: {objFolder.GetDetailsOf(item, i)}");
}
}
Console.ReadLine();
}
}
}
And this shows exactly what I expected in the console, but I want that to print that information to a text file instead.
I'm trying to work with this, though I don't know if this is the best way to do it:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApp2
{
class Program
{
[STAThread]
public static void Main(string[] args)
{
List<string> arrHeaders = new List<string>();
Shell32.Shell shell = new Shell32.Shell();
Shell32.Folder objFolder;
objFolder = shell.NameSpace(@"C:\testfolder\testprop");
for (int i = 0; i < short.MaxValue; i++)
{
string header = objFolder.GetDetailsOf(null, i);
if (String.IsNullOrEmpty(header))
break;
arrHeaders.Add(header);
}
foreach (Shell32.FolderItem2 item in objFolder.Items())
{
for (int i = 0; i < arrHeaders.Count; i++)
{
FileStream fs = new FileStream("C:\\testfolder\\testprop\\Test.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
Console.SetOut(sw);
Console.WriteLine($"{i}\t{arrHeaders[i]}: {objFolder.GetDetailsOf(item, i)}");
sw.Close();
}
}
}
}
}
And that will create a text file, but it only shows one line and it looks like the last attribute that would have shown in the console version.
I admittedly do not know what I'm doing, but does someone see a fix to print all lines from console to the text file instead of only the last line?