0

I am currently writing a language translator application. I want to support the ability to choose a file, read in the contents of the file into the entry box, and output the translation once the appropriate button is pressed.

I am using the plugin 'Xam.Plugin.FilePicker' to allow the user to choose a file, which is working. When the user chooses a file, I have it so that the name of the file is displayed on the screen. However, problems occur when trying to read in the file into the entry box, which I believe is linked to the application not being able to determine the path of the file - currently the application tries to read in the file from the location where Visual Studio 2017 is located.

I have tried several approaches, some of which are detailed in the below:

I have also tried:

var file = await CrossFilePicker.Current.PickFile();

if (file != null) {
    string filePath = 
    Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

    string filename = Path.Combine(filePath, file.FileName);

     using (var reader = new StreamReader(filename)) {
           entText= reader.ReadToEnd();

     }
}

Where entText is the name of the entry box in the MainPage.xaml.cs file

Below is the code I have so far. I have removed the above code from the file and, while it was giving me a path, the path was either the path in which Visual Studio 2017 is located in or some other path. Either way, the application couldn't find the file.

C# code:

private async void BtnReadFile_Clicked(object sender, EventArgs e)
{
    string fileName;
    string fileText;
    string filePath;

    // Allows the user to choose a file from any location
    var file = await CrossFilePicker.Current.PickFile();

    if (file != null)
    {
        lblFileRead.Text = file.FileName; // Displays the name of the file
    }
}

Xaml code:

  • Entry box:
<Entry x:Name="entText" Placeholder="Enter text to translate" Keyboard="Text" 
                   HeightRequest="200" WidthRequest="250" TextChanged="EntText_TextChanged" />
  • Button to read in file
<Button x:Name="btnReadFile" Text="Read in file" Clicked="BtnReadFile_Clicked" />

To conclude, I want to be able to read in a file from any location, not just a predetermined location i.e.

The user should be able to read in a file from "C:/Documents/files", "C:/Downloads/", etc.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • @mjwills While I am able to get the name of the file using the plugin, I am unable to read in the contents of the file into the entry box due the application not being able to determine the correct path of the file. The 'BtnReadFile_Clicked' function is where I am trying to read in the file. – Robert Hall Mar 28 '19 at 12:43
  • Why are you setting `filePath` to `Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)` rather than `file.FilePath`? – mjwills Mar 28 '19 at 12:47

3 Answers3

2

I guess you are using Xamarin? You are not supposed to have access to the file itself. You can get the name and you can get the contents.

You can get the file's contents by accessing file.DataArray instead of using the traditional file access. So what the actual path of the file is, is none of your business.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
1

You are setting filePath to

Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)

when it should instead be:

file.FilePath

since the latter is the actual location of the file.

mjwills
  • 23,389
  • 6
  • 40
  • 63
1

the CrossFilePicker returns a full path reference to the selected file. So you don't have to combine it with any other path.

Refer to the example from the project website It shows exactly what you try to do - read the file content and output it.

    try
    {
        FileData fileData = await CrossFilePicker.Current.PickFile();
        if (fileData == null)
            return; // user canceled file picking

        string fileName = fileData.FileName;
        string contents = System.Text.Encoding.UTF8.GetString(fileData.DataArray);

        System.Console.WriteLine("File name chosen: " + fileName);
        System.Console.WriteLine("File data: " + contents);
    }
    catch (Exception ex)
    {
        System.Console.WriteLine("Exception choosing file: " + ex.ToString());
    }

You just have to replace the Console output by putting the value of contents to your control.

Torben Schramme
  • 2,104
  • 1
  • 16
  • 28