StreamWriter 在 WriteLine 中间停止

StreamWriter stops in the middle of a WriteLine

我有一个小应用程序,它检查以域用户名命名的目录中的所有日志,并生成一个结果文件,其中包含每个用户名以及该用户的相关名字和姓氏。

控制台正在成功输出完整列表,但似乎 StreamWriter 在条目中途停止。

它在停止前写入的字符数在某种程度上是一致的。如果我将 outputstring 设置为在两个变量 filenameFindNameFromUsername 的结果之间包含更多字符,那么带或不带空格的字符数会发生变化,所以我已经排除了这一点。

关于为什么控制台输出该行但 streamwriter 不输出的任何想法?

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using System.DirectoryServices.ActiveDirectory;

namespace Filename_Finder
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please insert the full directory path. All filenames in the root of this folder will be logged to one file.");
            string outputFilename = "results.txt";
            string userPath = Console.ReadLine();
            string domain = "ou=users,dc=domain,dc=local";
            try
            {
                string outputFilepath = userPath + outputFilename;
                string[] filepaths = Directory.GetFiles(userPath);
                using (StreamWriter file = new StreamWriter(outputFilepath, false))
                {
                    for (int i = 0; i < filepaths.Length; i++)
                    {
                        string filepath = filepaths[i];
                        char split = '.';
                        string filename = filepath.Remove(0, userPath.Count());
                        if (filename != outputFilename)
                        {
                            int extensionBegins = filename.LastIndexOf(split);
                            filename = filename.Remove(extensionBegins);
                            string outputstring = (filename + " -- " + FindNameFromUsername(filename, domain));
                            Console.WriteLine(outputstring);
                            file.WriteLine(outputstring);
                        }
                    }
                    Console.ReadLine();
                }
            }
            catch (Exception e) { Console.WriteLine(e); Console.ReadLine(); }

        }

        public static string FindNameFromUsername(string username, string domainScope)
        {
            string connectionPrefix = "LDAP://" + domainScope;
            DirectoryEntry entry = new DirectoryEntry(connectionPrefix);
            DirectorySearcher searcher = new DirectorySearcher(entry);
            string filter = "(&(objectClass=user)";
            filter += "(|(sAMAccountName=" + username + ")))";
            searcher.Filter = filter;
            SearchResult result = searcher.FindOne();
            string resultValue = string.Empty;
            DirectoryEntry obj = result.GetDirectoryEntry();
            resultValue = "" + obj.Properties["givenName"].Value + " " + obj.Properties["sn"].Value;
            if (resultValue == " ") { resultValue = username; }
            entry.Close(); entry.Dispose();
            obj.Close(); obj.Dispose();
            searcher.Dispose();
            return resultValue;
        }

    }
}

您的 Console.ReadLine() 将暂停执行,我猜这就是您检查文件内容的地方。

但是,在 StreamWriter 被处理之前,文件不会 flushed and closed。这发生在 using 块的末尾,即在你的 ReadLine() 语句之后。

看看这个question

您可能会发现将 StreamWriter.AutoFlush 属性 设置为 true 很有用,以便获得与相应 Console 方法类似的行为,如下所示:

using (var file = new StreamWriter(outputFilepath, false) { AutoFlush = true })