这段代码给我输出“System.IO.FileInfo []”,它找不到我正在搜索的文件

时间:2017-10-02 09:39:54

标签: c# directory system.io.file

为什么这段代码返回System.IO.FileInfo []?而不是名为login.txt的项目列表

            Console.WriteLine("Searching for users wait...");
        string username;
        string password;
        var fileList = new DirectoryInfo("C:").GetFiles("login.txt", SearchOption.AllDirectories);
        string Lists = fileList.ToString();
        Console.WriteLine(Lists);
        var position = Lists.IndexOf("login");
        if (position == -1)
        {
            Console.WriteLine("No users Found.... FUCK!!");
            Console.WriteLine("Lets create a user. insert username");
            username = Console.ReadLine();
            Console.WriteLine("Add a password");
            password = Console.ReadLine();
            string loginData = username + "   " + password;
            try { System.IO.File.WriteAllText("C:", loginData); }
            catch (System.UnauthorizedAccessException)
            {

                Console.WriteLine("I have no access to create a login file...");
                Console.WriteLine("I will now to to gain access");
                Process proc = new Process();
                proc.StartInfo.FileName = "ConsoleApp2.exe";
                proc.StartInfo.UseShellExecute = true;
                proc.StartInfo.Verb = "runas";
                proc.Start();

            }
        }

我需要它来制作一个存储用户名和密码的txt文件,我还需要搜索该文件,但我认为我在这里做错了..

希望你能帮忙 感谢

1 个答案:

答案 0 :(得分:3)

打印集合(代码中为fileList

  // wrong way
  string Lists = fileList.ToString();
  Console.WriteLine(Lists);

您必须将其表示为string,例如在string.Join

的帮助下
  Console.WriteLine(string.Join(Environment.NewLine, fileList));

或实施循环

  foreach (var item in fileList)
    Console.WriteLine(item);