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!

Recommended Answers

All 8 Replies

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)"

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

if any one still trying to know, in the mobile device there is no C drive, so just write without it on the path.

    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("\My Documents\test.txt") '<-- change here     
            Entire = oRead.ReadToEnd()
            Label1.Text = Entire
        End Sub
    End Class
commented: Better late than never! +9
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.