-6

Why is this code returning System.IO.FileInfo[]? and not a list of items named 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();

            }
        }

I need it to make a txt file that stores a username and a password, and i also need to search for that file, but i think im making something wrong here..

hope you'll help thanks

1 Answers1

3

When printing out a collection (fileList in your code)

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

you have to either represent it as string, e.g. with a help of string.Join

  Console.WriteLine(string.Join(Environment.NewLine, fileList));

or implement a loop:

  foreach (var item in fileList)
    Console.WriteLine(item); 
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215