0

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?

phpwhatthertf
  • 83
  • 1
  • 10
  • 1
    There are lots of problems with your approach, but fundamentally the problem is that you are overwriting the file each iteration of your loop. See duplicate question. Beyond that: you absolutely should _not_ be changing the `Console.Out` writer, and really you shouldn't be reopening the file with each line of text either. Just create the `sw` object outside the loop, write each line to it inside the loop, and close it when you're done. Leave `Console` alone, and put the `sw` allocation inside a `using` statement so that it gets closed automatically, even if an exception occurs. – Peter Duniho Aug 16 '20 at 19:10
  • Thank you, I used that duplicate question (or one I found from that) and fixed it. And found a couple more getting even closer to what I ultimately need. – phpwhatthertf Aug 17 '20 at 22:11

0 Answers0