streamwriter没有写入文件

时间:2015-06-01 13:47:47

标签: c#

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);
           }
     }
 }

这是我的代码我正在尝试将文件的名称写入文件中。 该文件结果为空。虽然我打了file.writeline线。 你能看出我的错误吗? 我按照这个例子左右 https://msdn.microsoft.com/en-us/library/8bh11f1k.aspx

4 个答案:

答案 0 :(得分:3)

您发布的代码应该可以使用。

如果您在代码运行时尝试检查文件的内容(例如,如果您设置了断点),则流不会被刷新,这意味着它不会实际写入任何内容。档案。

如果要在调试期间查看文件的内容,则应在Flush之后调用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

答案 1 :(得分:1)

这不是答案 - 请在downvoting之前阅读上述评论!

将路径更改为适用于您系统的内容

tar.gz

答案 2 :(得分:1)

试试这个:

            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);
                }
            }
        }

或者这个:

            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 });
            }
        }

答案 3 :(得分:-3)

因此bottomline open是循环内的StreamWriter

 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);
               }
           }
         }