Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

Reading lines from txt file in Windows 8

$
0
0

I’m trying to read a specific line from a txt file which contains 3 lines, but for some reason my code showing an error when I run it, can somebody please direct me to resolve this issue.

The code:

                Dim GeneralFolder As StorageFolder = Await localFolder.CreateFolderAsync("General", CreationCollisionOption.ReplaceExisting)
                Dim WriteToFile As StorageFile = Await GeneralFolder.CreateFileAsync("dataFile.txt", CreationCollisionOption.ReplaceExisting)
                Await FileIO.WriteTextAsync(WriteToFile, "line number 1")
                Await FileIO.WriteTextAsync(WriteToFile, "line number 2")
                Await FileIO.WriteTextAsync(WriteToFile, "line number 3")

                Dim ReadFromFile As StorageFile = Await GeneralFolder.GetFileAsync("dataFile.txt")
                If ReadFromFile IsNot Nothing Then
                    Dim LineOne As String() = Await FileIO.ReadLinesAsync(ReadFromFile)

                    Dim md As New Windows.UI.Popups.MessageDialog(LineOne(0), "message")
                    md.Commands.Add(New Windows.UI.Popups.UICommand("OK"))
                    Await md.ShowAsync()
                End If

The error is:

-----------------------------------

Unable to cast COM object of type 'System.__ComObject' to class type 'System.Object[]'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.

------------------------------------

This error is pointing to

Dim LineOne As String() = Await FileIO.ReadLinesAsync(ReadFromFile)

Thank you all in advance


Eng. Ash


Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>