0

I'm getting this error when I try to open the file.The file path does exist i copied from the explorer to make sure there was no error in the path string.

System.IO.FileNotFoundException: Could not find file "/C:\Users\Mario\Documents\ativita\ativita\produtos_20180713.CSV"

List <banco_produtos> csv = File.ReadAllLines(Path.GetDirectoryName("C:\\Users\\Mario\\Documents\\ativita\\ativita\\produtos_20180713.CSV"))
                       .Skip(1)
                       .Select(c => c.Split(','))
                       .Select(x => new //there some code here 

                       })
Mario
  • 103
  • 1
  • 9
  • 3
    Why are you calling `Path.GetDirectoryName`? –  Aug 02 '18 at 13:19
  • 3
    if this is a Xamarin app, then it cannot read a file that is located on the desktop. It can only read files that are located in the file system of the device/emulator that they are running on. – Jason Aug 02 '18 at 13:21
  • 1
    @mjwills If this is Xamarin the target may be a Unix-like system, in which case I can plausibly see `Path.GetDirectoryName` putting a `/` on the start of a Windows-style path. – Rawling Aug 02 '18 at 13:26
  • there is no "/" before the C the code is adding it. – Mario Aug 02 '18 at 13:36
  • @Jason and how I get the file path if I put inside the emulator? I can do the same as win"c:\\Users" but the path inside the emultator? – Mario Aug 02 '18 at 13:40
  • You need to copy the file into your project at build time, or read it from a server at runtime. See https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/files?tabs=vswin – Jason Aug 02 '18 at 13:47
  • Possible duplicates [stack overflow - xamarin.forms load file](https://www.google.com/search?q=xamarin.forms+load+file+site:stackoverflow.com) – Igor Aug 02 '18 at 15:38
  • I have found the answer in this link: [https://stackoverflow.com/questions/48043058/xamarin-forms-read-from-text-file-result-is-null](https://stackoverflow.com/questions/48043058/xamarin-forms-read-from-text-file-result-is-null) – Mario Aug 02 '18 at 16:47

2 Answers2

-1

First of all you have given the full path and you should remove Path.GetDirectoryName from your code:

   banco_produtos[] csv = File.ReadAllLines("C:\\Users\\Mario\\Documents\\ativita\\ativita\\produtos_20180713.CSV")
                           .Skip(1)
                           .Select(c => c.Split(','))
                           .Select(x => new //there some code here 

                           });

2- ReadAllLines(), returns an array not a list so either you have to use banco_produtos[] csv or use .ToList() to convert it to a List.

Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
-3

Seems like this is a problem with characters not being escaped. Try setting the path to a variable, then pass in the variable to your GetDirectoryName call.

Try:

var path = @"C:\Users\Mario\Documents\ativita\ativita\";


List <banco_produtos> csv = File.ReadAllLines(Path.GetDirectoryName(path))
                       .Skip(1)
                       .Select(c => c.Split(','))
                       .Select(x => new //there some code here 

                       })
hmiedema9
  • 948
  • 4
  • 17
  • You can't read lines from a *file* if you pass the name of a *directory* in. `Path.GetDirectoryName` should be removed *entirely*. The OP did escape their path correctly. –  Aug 02 '18 at 13:24
  • what I'm doing right now is using the comment from @jason and did this.Added the file to the project and embended to the resource and the code: `var assembly = IntrospectionExtensions.GetTypeInfo(typeof(novo_produto)).Assembly; Stream stream = assembly.GetManifestResourceStream("ativita.Class1.txt");` but now "stream" returns null. – Mario Aug 02 '18 at 14:53
  • Edited my comment to reflect a directory instead of a file. My mistake for not realizing OP was trying to read lines from a "file" using `Path.GetDirectoryName`. Was confused as to what OP was after. – hmiedema9 Aug 02 '18 at 16:26