Hello. im trying to creat a pocket pc application using visual basic. but i got problem access text file. system always shows that "counld not find a part of path". the code i wrote is like:

Imports System
Imports System.IO

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim oRead As System.IO.StreamReader
        Dim Entire As String

        oRead = IO.File.OpenText("C:\Documents and Settings\test.txt")        Entire = oRead.ReadToEnd()
        Label1.Text = Entire

    End Sub
End Class

(the red code is the one with error)

Please Help! Thx!

Edited 6 Years Ago by __avd: Added [code] tags.

Try to replace the part in red with this.
Although, I don't know if it works on Pocket PC devices.

oRead = IO.File.OpenText(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\test.txt")
Entire = oRead.ReadToEnd()

My.Computer.FileSystem.SpecialDirectories.MyDocuments is a VB.NET shortcut to the "C:\Documents and Settings\<current user>\My Documents" folder.

Try to replace the part in red with this.
Although, I don't know if it works on Pocket PC devices.

oRead = IO.File.OpenText(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\test.txt")
Entire = oRead.ReadToEnd()

My.Computer.FileSystem.SpecialDirectories.MyDocuments is a VB.NET shortcut to the "C:\Documents and Settings\<current user>\My Documents" folder.

Thx for ur help!
i tried, but shows that 'Computer' is not a member of 'My'
if i delete the 'My' will also show that 'Computer' is not declared.

Ok.
Let's try replacing "My.Computer.FileSystem.SpecialDirectories.MyDocuments" with "System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)"

Edited 6 Years Ago by Oxiegen: n/a

Ok.
Let's try replacing "My.Computer.FileSystem.SpecialDirectories.MyDocuments" with "System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)"

this time the computer said that 'MyDocuments' is not a member of 'System.Environment.SpecialFolder'. Under the SpecialFolder have: ApplicationData, Favourites, Personal, Programs, StartMenu, StartUp.
i try to use the Personal, and have saved the test.txt file in the My Documents folder. System shows that could not find the file "\My Documents\test.txt"

Very odd. On Pocket PCs, the Personal folder is the same as My Documents.
I found this code snippet on MSDN for accessing Special Folders in .NET Compact Framework.

Public Shared Function GetFolderPath(folder As SpecialFolder) As String
   'Buffer to fill with path
   Dim path As StringBuilder = New StringBuilder(255)

   'Pass stringbuilder and folder identifier to api function
   If Not Convert.ToBoolean(SHGetSpecialFolderPath(IntPtr.Zero, path, CInt(folder), 0)) Then
      Throw New Exception("Cannot get folder path")
   End If
   Return path.ToString()
End Function

<DllImport("coredll", EntryPoint:="SHGetSpecialFolderPath", SetLastError:=False)> _
Private Shared Function SHGetSpecialFolderPath(hwndOwner As IntPtr, lpszPath As StringBuilder, nFolder As Integer, fCreate As Integer) As Integer
End Function

Very odd. On Pocket PCs, the Personal folder is the same as My Documents.
I found this code snippet on MSDN for accessing Special Folders in .NET Compact Framework.

Public Shared Function GetFolderPath(folder As SpecialFolder) As String
   'Buffer to fill with path
   Dim path As StringBuilder = New StringBuilder(255)

   'Pass stringbuilder and folder identifier to api function
   If Not Convert.ToBoolean(SHGetSpecialFolderPath(IntPtr.Zero, path, CInt(folder), 0)) Then
      Throw New Exception("Cannot get folder path")
   End If
   Return path.ToString()
End Function



<DllImport("coredll", EntryPoint:="SHGetSpecialFolderPath", SetLastError:=False)> _
Private Shared Function SHGetSpecialFolderPath(hwndOwner As IntPtr, lpszPath As StringBuilder, nFolder As Integer, fCreate As Integer) As Integer
End Function

Hi, thx for ur patient...
i tried use the Personal under the Environment.SpecialFolder. (Ya, Personal is equal to My Documents. and i tried the one you gave me, accessing the specialfolder. even that, still got error said My Documents is not a member of specialfolder.) So now have the error that Could not find the file "\My Documents\test.txt".
P.S. i connected the pocket PC with computer and put the test.txt file in the Mobile Device folder.

If you browse the filesystem on your Pocket PC, can you see the text file in the My Documents folder?
If not, then perhaps this could be useful in order to avoid errors:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim oRead As System.IO.StreamReader
        Dim Entire As String

        If IO.File.Exists(GetFolderPath(5) & "\test.txt") Then
            oRead = IO.File.OpenText(GetFolderPath(5) & "\test.txt")
            Entire = oRead.ReadToEnd()
            Label1.Text = Entire
        End If
    End Sub
This article has been dead for over six months. Start a new discussion instead.