2 Replies - 259 Views - Last Post: 28 September 2013 - 06:27 AM Rate Topic: -----

#1 Kajsa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-September 13

Searching for music files in folder - How to set ID on them?

Posted 28 September 2013 - 04:48 AM

Greetings fellow C# Hackers!

I am new to C# coding, so please be gentle with me :flowers:/>/>/>

The question:
I am trying to build a ConsoleApplication in C# where i go to a folder C:\music\, getting asked to search for what name on files i want to play. And then once the program recive the input
(1 - 10 for example). It will play the file.


I have been able to get the code to the search function work, but i have no idea how i should do once the files are listed and then set an value/ID to them so i can just type in a number and it will start to play. :unsure:/>/>/>

I have this code for the Search function:

//read the file system contents
            DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\music\");

            FileInfo[] fileNames = directoryInfo.GetFiles("*.*");
            Dictionary<int, string> fileDictionary = new Dictionary<int, string>();
            //store the files
            for (int i = 0; i < fileNames.Length; i++)
            {
                // use Name here so user doesn't need to enter full path of file for a full match
                fileDictionary.Add(i, fileNames[i].Name);
            }
            //get user input
            string fileToFind;
            do
            {
                Console.WriteLine("Please enter the full or partial file name: ");
                fileToFind = Console.ReadLine().Trim();
            }
            while (fileToFind == "");
            //search!
            string[] foundFiles = fileDictionary.Values.Where(s => s.Contains(fileToFind)).ToArray();

            if (foundFiles.Length > 0)
            {
                Console.WriteLine("Found {0} File(s) as follows :\n", foundFiles.Length);
                foreach (string foundFile in foundFiles) Console.WriteLine("  {0}", foundFile);
            }
            else
            {
                Console.WriteLine("No files found.");
            }


And i want it to look something like this:

http://s13.postimg.org/h19jyoy4n/Capture.png

Any ideas how i can set ID/numbers on the items in my array and then use a number to select what song i would like to play?

Many thanks

Kajsa

This post has been edited by tlhIn`toq: 29 September 2013 - 11:04 AM
Reason for edit:: Don't refer to developers as hackers: Its rude.


Is This A Good Question/Topic? 0
  • +

Replies To: Searching for music files in folder - How to set ID on them?

#2 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 349
  • View blog
  • Posts: 1,506
  • Joined: 08-April 09

Re: Searching for music files in folder - How to set ID on them?

Posted 28 September 2013 - 06:15 AM

To start use Process.Start Method and pass the ID/number as parameter
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3243
  • View blog
  • Posts: 10,882
  • Joined: 12-December 12

Re: Searching for music files in folder - How to set ID on them?

Posted 28 September 2013 - 06:27 AM

A simplistic approach is just to add sequential numbers to the output:

    if (foundFiles.Length > 0) {
        Console.WriteLine("Found {0} File(s) as follows :\n", foundFiles.Length);
        int x = 1;
        foreach (string foundFile in foundFiles) Console.WriteLine("{0}  {1}", x++, foundFile);
    } else {
        Console.WriteLine("No files found.");
    }


If the user enters a number 4 then the item can be retrieved using:

foundFiles[4 - 1]

you have to subtract 1 because arrays are indexed starting at 0. This retrieves the filename so you need to prepend it with "C:\music\".

This is not the best approach though, but it is something to work with.

This post has been edited by andrewsw: 28 September 2013 - 06:34 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1