Question

when I run this code:

static void Main(string[] args)
    {
        var currentDirectory = Directory.GetCurrentDirectory();
        var searchDirectory = new DirectoryInfo(currentDirectory);

        var queryMatchingFiles =
                from file in searchDirectory.GetFiles()
                let fileContent = System.IO.File.ReadAllText(file.Name)
                select file.Name;

        StreamWriter outputCacheMeta = new StreamWriter(@"output.txt");

        foreach (var fileName in queryMatchingFiles.Where(fileName => !fileName.EndsWith(".txt") && !fileName.EndsWith(".exe") && !fileName.EndsWith(".xz")))
        {
            // start the converion utility
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = "xz.exe";
            startInfo.Arguments = "-k -z " + fileName;
            startInfo.CreateNoWindow = true;
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;

            using (Process p = Process.Start(startInfo))
            {
                while (!p.HasExited)
                {
                    Thread.Sleep(300);
                }
            }

            //Process.Start(startInfo);

            Console.WriteLine(string.Format("Compressing file: '{0}'", fileName.ToString()));

            // generate final string
            FileInfo inFile = new FileInfo(fileName);
            FileInfo outFile = new FileInfo(fileName + ".xz");

            outputCacheMeta.WriteLine("<ContentFile Name=\"" + fileName.ToString() + "\" Size=\"" + inFile.Length.ToString() + "\" SHA1Hash=\"" + HashCalc.GetSHA1Hash(fileName).ToString() + "\" CompressedSize=\"" + outFile.Length.ToString() + "\" />");
            //Console.WriteLine(string.Format(("<ContentFile Name=\"" + fileName.ToString() + "\" Size=\"" + inFile.Length.ToString() + "\" SHA1Hash=\"" + HashCalc.GetSHA1Hash(fileName).ToString() + "\" CompressedSize=\"" + outFile.Length.ToString() + "\" />")));
        }
    }

it does not print everything in the output file (output.txt), it prints this: http://pastebin.com/1vTQZVih (sorry for external link).

The problem is that it suddenly 'stops' writing to the output file.

Thanks!

Was it helpful?

Solution

You are not Flush()ing or Close()ing your StreamWriter before your program exits. Some of your file data is buffered to be written to the file, but will not actually get written until your flush and and close the stream.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top