Gilad
Gilad

Reputation: 6595

streamwriter not writing into the file

string directoryPath = @"D:\Tools\Examples";
using (StreamWriter file = new StreamWriter(@"C:\Users\gdarmon\Desktop\MacbethTest.txt",true))
{
    foreach (string rawImagePath in Directory.GetFiles(directoryPath, "*.raw").Select(Path.GetFullPath))
    {
       image.ReadHeaderLessImage(rawImagePath, width, height, colorOrder, bpp);

       List<MacbethCheckerBatchesColor> resultWithOutExtraLines =
       wrapper.DetectMacbethColorChecker(image.Data, image.Width, image.Height, image.ColorOrder,
                                         image.Bpp, showResult, batchFillFactor);
           if (resultWithOutExtraLines == null)
           {
                file.WriteLine(rawImagePath);
           }
     }
 }

here is my code I'm trying to write the name of the files which are not working well into a file. the file turns out empty. althought I hit the file.writeline line.
can you see my mistake?
I followed this example
https://msdn.microsoft.com/en-us/library/8bh11f1k.aspx

Upvotes: 2

Views: 277

Answers (4)

OlegSoft
OlegSoft

Reputation: 91

Try this:

            string directoryPath = @"D:\Tools\Examples";
        using (StreamWriter file = new StreamWriter(@"C:\Users\gdarmon\Desktop\MacbethTest.txt", true) { AutoFlush = true })
        {
            foreach (string rawImagePath in Directory.GetFiles(directoryPath, "*.raw").Select(t => Path.GetFullPath(t)))
            {
                image.ReadHeaderLessImage(rawImagePath, width, height, colorOrder, bpp);

                List<MacbethCheckerBatchesColor> resultWithOutExtraLines =
                wrapper.DetectMacbethColorChecker(image.Data, image.Width, image.Height, image.ColorOrder,
                                                  image.Bpp, showResult, batchFillFactor);
                if (resultWithOutExtraLines == null)
                {
                    file.WriteLine(rawImagePath);
                }
            }
        }

Or this:

            string directoryPath = @"D:\Tools\Examples";
        foreach (string rawImagePath in Directory.GetFiles(directoryPath, "*.raw").Select(Path.GetFullPath))
        {
            image.ReadHeaderLessImage(rawImagePath, width, height, colorOrder, bpp);
            List<MacbethCheckerBatchesColor> resultWithOutExtraLines =
            wrapper.DetectMacbethColorChecker(image.Data, image.Width, image.Height, image.ColorOrder,
                                              image.Bpp, showResult, batchFillFactor);
            if (resultWithOutExtraLines == null)
            {
                File.AppendAllLines(@"C:\Users\gdarmon\Desktop\MacbethTest.txt", new[] { rawImagePath });
            }
        }

Upvotes: 1

thomasb
thomasb

Reputation: 6045

The code you have posted should work.

If you're trying to check the contents of the file while the code is running (for instance if you have set a breakpoint), the stream will not have been flushed, meaning it won't have actually written anything to the file yet.

If you want to see the contents of the file during debug, you should call the Flush method after WriteLine : https://msdn.microsoft.com/fr-fr/library/system.io.streamwriter.flush(v=vs.110).aspx

 file.WriteLine(rawImagePath);
 file.Flush();
 // add a breakpoint after here: the file will have contents

Upvotes: 3

Lee Willis
Lee Willis

Reputation: 1582

This is not an answer - please read the comments above before downvoting!

Change the paths to something that works on your system

        static void Main (string[] args) {

            string directoryPath = @"F:\logs";
            using (StreamWriter file = new StreamWriter(@"f:\development\testing.txt", true)) {
                foreach (string rawImagePath in Directory.GetFiles(directoryPath, "*.log").Select(Path.GetFullPath)) {
                    //image.ReadHeaderLessImage(rawImagePath, width, height, colorOrder, bpp);

/*
                    List<MacbethCheckerBatchesColor> resultWithOutExtraLines =
                    wrapper.DetectMacbethColorChecker(image.Data, image.Width, image.Height, image.ColorOrder,
                                                      image.Bpp, showResult, batchFillFactor);
*/
                    //if (resultWithOutExtraLines == null) {
                        file.WriteLine(rawImagePath);
                    //}
                }
            }
        }
    }

Upvotes: 1

Amit Kumar Ghosh
Amit Kumar Ghosh

Reputation: 3726

So the bottomline is open the StreamWriter inside the loop.

 string directoryPath = @"D:\Tools\Examples";

        foreach (string rawImagePath in Directory.GetFiles(directoryPath, "*.raw").Select(Path.GetFullPath))
        {
          using (StreamWriter file = new StreamWriter(@"C:\Users\gdarmon\Desktop\MacbethTest.txt",true))
           {
           image.ReadHeaderLessImage(rawImagePath, width, height, colorOrder, bpp);

           List<MacbethCheckerBatchesColor> resultWithOutExtraLines =
           wrapper.DetectMacbethColorChecker(image.Data, image.Width, image.Height, image.ColorOrder,
                                             image.Bpp, showResult, batchFillFactor);
               if (resultWithOutExtraLines == null)
               {
                    file.WriteLine(rawImagePath);
               }
           }
         }

Upvotes: -3

Related Questions