StreamWriter在WriteLine中间停止

时间:2015-09-23 11:09:07

标签: c# io console-application streamwriter

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

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

在停止之前写入的字符数在某种程度上是一致的。如果我将outputstring设置为在两个变量filenameFindNameFromUsername的结果之间包含更多字符,则包含或不包含空格的字符数会发生变化,因此我已将其排除在外。

关于为什么控制台输出线但输出器没有输出的任何想法?

以下代码:

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

    }
}

2 个答案:

答案 0 :(得分:1)

您的Console.ReadLine()将暂停执行,并且我猜测您正在检查文件内容的位置。

但是,在StreamWriter处置之前,该文件不会是flushed and closed。这发生在using块的末尾,即在您的ReadLine()声明之后。

看看这个question

答案 1 :(得分:0)

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

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