0

当我运行此代码时:

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() + "\" />")));
        }
    }

它不会打印输出文件(output.txt)中的所有内容,它会打印: http: //pastebin.com/1vTQZVih(对不起,外部链接)。

问题是它突然“停止”写入输出文件。

谢谢!

4

1 回答 1

2

在您的程序退出之前,您不是Flush()ing 或Close()ing your 。StreamWriter您的一些文件数据被缓冲以写入文件,但在您刷新并关闭流之前实际上不会被写入。

于 2013-11-13T19:29:45.537 回答