c# - StreamWriter 在 WriteLine 中间停止

标签 c# io console-application streamwriter

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

控制台正在成功输出完整列表,但似乎 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() 将暂停执行,我猜这就是您检查文件内容的地方。

但是,文件不会是 flushed and closed直到 StreamWriter 被释放。这发生在 using block 的末尾,即在您的 ReadLine() 语句之后。

看看这个question .

关于c# - StreamWriter 在 WriteLine 中间停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32737966/

相关文章:

c# - 在 C# 中使用 Json.NET 计算 JSON 字符串中的元素数

c# - 如何在 Orchard 中根据字符串名称创建形状?

xcode - 将静态库添加到 XCode 控制台应用程序

c++ - 在应用程序启动时设置控制台窗口特定大小

c# - Azure 文件共享属性 LastModified 的 null 值

C# 在相应的输入类型中返回零

c++ - 防止输入小于 0 的字母或数字

java - 无法使用 Files.walkFileTree java nio 打印目录内的子目录

java - java中ObjectInputStream中的IOException

c# - 如何在 .NET 控制台应用程序中获取应用程序的路径?