C# 流写器停在书写线的中间
我有一个小应用程序,它检查以域名命名的目录中的所有日志,并生成一个结果文件,其中包含每个用户名以及该用户的相关名字和姓氏 控制台正在成功输出完整列表,但是C# 流写器停在书写线的中间,c#,io,console-application,streamwriter,C#,Io,Console Application,Streamwriter,我有一个小应用程序,它检查以域名命名的目录中的所有日志,并生成一个结果文件,其中包含每个用户名以及该用户的相关名字和姓氏 控制台正在成功输出完整列表,但是StreamWriter似乎在输入的中途停止 它在停止之前写入的字符数在某种程度上是一致的。如果我将outputstring设置为在两个变量filename和FindNameFromUsername的结果之间包含更多字符,那么带空格或不带空格的字符数会发生变化,因此我排除了这种情况 关于为什么控制台输出该行而streamwriter不输出该行,
StreamWriter
似乎在输入的中途停止
它在停止之前写入的字符数在某种程度上是一致的。如果我将outputstring
设置为在两个变量filename
和FindNameFromUsername
的结果之间包含更多字符,那么带空格或不带空格的字符数会发生变化,因此我排除了这种情况
关于为什么控制台输出该行而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;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.IO;
使用System.Threading.Tasks;
使用System.DirectoryServices;
使用System.DirectoryServices.AccountManagement;
使用System.DirectoryServices.ActiveDirectory;
名称空间文件名查找器
{
班级计划
{
静态void Main(字符串[]参数)
{
Console.WriteLine(“请插入完整的目录路径。此文件夹根目录中的所有文件名都将记录到一个文件中。”);
字符串outputFilename=“results.txt”;
字符串userPath=Console.ReadLine();
string domain=“ou=users,dc=domain,dc=local”;
尝试
{
字符串outputFilepath=userPath+outputFilename;
string[]filepath=Directory.GetFiles(userPath);
使用(StreamWriter文件=新StreamWriter(outputFilepath,false))
{
for(int i=0;i
您的控制台.ReadLine()
将暂停执行,我猜这就是您检查文件内容的地方
但是,在处置StreamWriter
之前,不会删除该文件。这发生在使用块的末尾,即在ReadLine()
语句之后
看看这个。您可能会发现将属性设置为true
很有用,以便获得与相应的控制台
方法类似的行为,如下所示:
using (var file = new StreamWriter(outputFilepath, false) { AutoFlush = true })
哇!这就成功了,我拿走了ReadLine。谢谢你的帮助!