0

So I'm very new at making apps (or GUI programs in general) but I felt like I could take on this challenge as I'm decently adept at python and VBA. I'm trying to make a visual schedule builder app for my university using a windows 8 app running in C# and xaml. The data for all courses is split up into .csv files, one for each department to make searching easier. These files are to be included in the assets of the app itself as I don't want to have to figure out how to host the data remotely right now (unless that would make this task easier).

I'm having trouble extracting the data from these files into my app. In my app there are currently two comboboxes that I'm working on. One for the department and one for the courses within that department, such as MECH 2202 for a specific class in mechanical engineering. I want that when the first combo is selected for MECH, the second combo populates with all different course codes in that department which are in a single .csv file. More details would be used later when the course code is chosen as there's several sections for each, though that can be figured out later.

The relevant xaml for testing purposes is the following. The numbers populating the second box are just temporary.

        <ComboBox x:Name="departmentCombo" HorizontalAlignment="Center" Margin="0" Grid.Row="1" VerticalAlignment="Center" Width="100" Height="40" MaxDropDownHeight="400" SelectionChanged="DepartmentChange">
            <ComboBoxItem Content="BIOE"/>
            <ComboBoxItem Content="CIVL"/>
            <ComboBoxItem Content="ECE"/>
            <ComboBoxItem Content="ENG"/>
            <ComboBoxItem Content="MECH"/>
        </ComboBox>
        <ComboBox x:Name="courseCombo" HorizontalAlignment="Center" Margin="0" Grid.Row="1" VerticalAlignment="Center" Width="100" Height="40" MaxDropDownHeight="400">
            <ComboBoxItem Content="2012"/>
            <ComboBoxItem Content="2202"/>
            <ComboBoxItem Content="3542"/>
            <ComboBoxItem Content="3582"/>
            <ComboBoxItem Content="3980"/>
        </ComboBox>

For example, all of the mechanical engineering courses are located in a single .csv file named "Course List_MECH.csv" located in the assets of the app. The data is structured as follows:

    department,course_code,[several other non-relevant fields]

My C# code so far for the event after a department has been chosen is this:

    private void DepartmentChange(object sender, SelectionChangedEventArgs e)
    {

        ComboBoxItem typeItem = (ComboBoxItem)departmentCombo.SelectedItem;
        string department_str = typeItem.Content.ToString();

        testText.Text = department_str;

        ///broken
        var reader = new StreamReader(File.OpenRead(@"C:\test.csv"));
        List<string> listA = new List<string>();
        List<string> listB = new List<string>();
        while (!reader.EndOfStream)
        {
            var line = reader.ReadLine();
            var values = line.Split(';');

            listA.Add(values[0]);
            listB.Add(values[1]);
        }
    }

The first part of this code functions to extract the chosen department and goes into a testing textblock to make sure it's working. So I wouldn't have any trouble formatting the file name to locate the correct department's file. Problem is everything after the comment is broken and doesn't work. I pulled this piece from Reading CSV file and storing values into an array. I'm not adept enough at C# to see why it's not even compiling. It says that "The name 'File' does not exist in the current context". Once I have all the course codes into an array I'm sure I could get it to populate the second combo box without too much trouble, it's getting the data into those arrays (or lists might be better) that I can't figure out.

Any help would be greatly appreciated. I have searched for an answer to this but nothing I get is clear enough for me to understand being new at C# and no code snippets have worked.

Community
  • 1
  • 1
Graham
  • 113
  • 2
  • 12

1 Answers1

0

If it is a Windows 8 App, you cannot read directly from a path you want and less from C:. you have to use a FileOpenPicker to have access to files

var picker = new FileOpenPicker() { SuggestedStartLocation = PickerLocationId.DocumentsLibrary};
picker.FilterType.Add(".csv");

var file = await picker.PickSingleFileAsync();
if(file!=null)
{
    var content = await FileIO.ReadTextAsync(file);
}

And now you can process the content, you can split first with '\r' or instead FileIO use the StreamReader.

It is possible to read files that are inside the project, add the csv file as content and then

string filePath = @"ms-appx:///test.csv";
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(filePath));
var content = await FileIO.ReadTextAsync(file);

and then process the file

Juan Pablo Garcia Coello
  • 3,192
  • 1
  • 23
  • 33
  • Your second piece of code looks quite simple and what I was looking for, I'll try it when I get back to my house later tonight. Would you be able to tell me how I would process the file? Would I just use the second part of the code I posted (after the line where I declared a variable named reader)? Or is there a better way which would be appended to your code to get the data into a list? If this was python I would do <> <> and it would be done for a list of all the content after the first comma. – Graham Jul 21 '15 at 19:27
  • Okay I've tried it. I get an error saying the file was not found. my filePath string is @"ms-appx:///Assets/CourseList_MECH.csv". I've verified that the file is located inside the assets folder of the project, along with the default splash images. I've tried moving it around, renaming it, nothing works. – Graham Jul 21 '15 at 22:40
  • In the properties of the file you have to set it to Content – Juan Pablo Garcia Coello Jul 22 '15 at 06:47
  • Thanks for your help! After a bunch of tinkering I got it to work and my comboboxes can fill. – Graham Jul 22 '15 at 22:49
  • In case the answer took you to solve the question could you mark it as answer? that helps people to know to follow the answers or not, thanks! – Juan Pablo Garcia Coello Jul 23 '15 at 07:14