Getting a file from the mydocuments folder :o

Jerre

New member
Joined
Dec 5, 2006
Messages
4
Location
Belgium => Hove
Programming Experience
Beginner
I'm making a game as a project for my seminary for school
First I'll show the code I have:

VB.NET:
Public Class Level1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Guy As System.Windows.Forms.PictureBox
    Friend WithEvents Skull As System.Windows.Forms.PictureBox
    Friend WithEvents Key As System.Windows.Forms.PictureBox
    Friend WithEvents Back As System.Windows.Forms.PictureBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Level1))
        Me.Guy = New System.Windows.Forms.PictureBox
        Me.Skull = New System.Windows.Forms.PictureBox
        Me.Key = New System.Windows.Forms.PictureBox
        Me.Back = New System.Windows.Forms.PictureBox
        Me.SuspendLayout()
        '
        'Guy
        '
        Me.Guy.Image = CType(resources.GetObject("Guy.Image"), System.Drawing.Image)
        Me.Guy.Location = New System.Drawing.Point(280, 280)
        Me.Guy.Name = "Guy"
        Me.Guy.Size = New System.Drawing.Size(20, 20)
        Me.Guy.TabIndex = 0
        Me.Guy.TabStop = False
        '
        'Skull
        '
        Me.Skull.Image = CType(resources.GetObject("Skull.Image"), System.Drawing.Image)
        Me.Skull.Location = New System.Drawing.Point(40, 20)
        Me.Skull.Name = "Skull"
        Me.Skull.Size = New System.Drawing.Size(20, 20)
        Me.Skull.TabIndex = 1
        Me.Skull.TabStop = False
        '
        'Key
        '
        Me.Key.BackgroundImage = CType(resources.GetObject("Key.BackgroundImage"), System.Drawing.Image)
        Me.Key.Location = New System.Drawing.Point(100, 180)
        Me.Key.Name = "Key"
        Me.Key.Size = New System.Drawing.Size(20, 20)
        Me.Key.TabIndex = 2
        Me.Key.TabStop = False
        '
        'Back
        '
        Me.Back.BackgroundImage = CType(resources.GetObject("Back.BackgroundImage"), System.Drawing.Image)
        Me.Back.Location = New System.Drawing.Point(0, 0)
        Me.Back.Name = "Back"
        Me.Back.Size = New System.Drawing.Size(300, 300)
        Me.Back.TabIndex = 3
        Me.Back.TabStop = False
        '
        'Level1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(300, 300)
        Me.Controls.Add(Me.Skull)
        Me.Controls.Add(Me.Guy)
        Me.Controls.Add(Me.Key)
        Me.Controls.Add(Me.Back)
        Me.MaximizeBox = False
        Me.MaximumSize = New System.Drawing.Size(308, 334)
        Me.MinimizeBox = False
        Me.MinimumSize = New System.Drawing.Size(308, 334)
        Me.Name = "Level1"
        Me.Text = "Level 1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
        Dim PrevTop As Single
        Dim PrevLeft As Single

        PrevTop = Guy.Top
        PrevLeft = Guy.Left
                ' Up: Het ventje gaat naar boven
        If e.KeyCode = Keys.Up Then
            Guy.Image = Image.FromFile("C:\Documents and Settings\Duré\Mijn documenten\Visual Studio Projects\Regicide\GuyUp.png")
            Guy.Top = Guy.Top - 20
            If Guy.Top < 0 Then
                Guy.Top = 0
            End If
        End If

        ' Down: Het ventje gaat naar beneden
        If e.KeyCode = Keys.Down Then
            Guy.Image = Image.FromFile("C:\Documents and Settings\Duré\Mijn documenten\Visual Studio Projects\Regicide\GuyDown.png")
            Guy.Top = Guy.Top + 20
            If Guy.Top > 280 Then
                Guy.Top = 280
            End If
        End If

        ' Left: Het ventje gaat naar links
        If e.KeyCode = Keys.Left Then
            Guy.Image = Image.FromFile("C:\Documents and Settings\Duré\Mijn documenten\Visual Studio Projects\Regicide\GuyLeft.png")
            Guy.Left = Guy.Left - 20
            If Guy.Left < 0 Then
                Guy.Left = 0
            End If
        End If

        ' Right: Het ventje gaat naar rechts
        If e.KeyCode = Keys.Right Then
            Guy.Image = Image.FromFile("C:\Documents and Settings\Duré\Mijn documenten\Visual Studio Projects\Regicide\GuyRight.png")
            Guy.Left = Guy.Left + 20
            If Guy.Left > 280 Then
                Guy.Left = 280
            End If
        End If

        '--------------------------
        '--------------------------
        Dim Levl1 As New Level1
        If LvlNow = 0 Then
            If Guy.Left = Key.Left And Guy.Top = Key.Top Then
                Key.Visible = False
                KeyOwned = True
            End If

            If Guy.Left = 280 And Guy.Top = 280 Or Guy.Left = 0 And Guy.Top = 0 Then
                Guy.Left = PrevLeft
                Guy.Top = PrevTop
            End If
            If Guy.Left = 0 And Guy.Top = 20 Then
                Back.Image = Image.FromFile("C:\Documents and Settings\Duré\Mijn documenten\Visual Studio Projects\Regicide\13.jpg")
                Guy.Left = 260
                Guy.Top = 20
                LvlNow = 1
            End If
        End If
        If LvlNow = 1 Then
            If Guy.Left = 280 And Guy.Top = 20 Then
                Back.Image = Image.FromFile("C:\Documents and Settings\Duré\Mijn documenten\Visual Studio Projects\Regicide\Level1a.bmp")
                Guy.Left = 20
                Guy.Top = 20
                LvlNow = 0
            End If
        End If
    End Sub

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

    End Sub
End Class
You might have noticed the "Back.Image = Image.FromFile("C:\Documents and Settings\Duré\Mijn documenten\Visual Studio Projects\Regicide\Level1a.bmp")" still refers to a file on my computer. What I need to do, is to let it refer to a "my documents" directory on whichever computer and then let the image change to the "Regicide\Resources\Level1a.bmp" which will be in the "my documents" directory. Even if the file is in the (in my case, because the language on my computer is dutch and not english) "Mijn documenten" folder.


Please help me if you can. :confused:
 
Environment.GetFolderPath(Environment.SpecialFolder.Personal) will return the 'My Documents' folder for current logged in user (doesnt matter if windows is using English or another language):
VB.NET:
Back.Image = Image.FromFile(Environment.GetFolderPath(Environment.SpecialFolder.Personal) & "\Visual Studio Projects\Regicide\Level1a.bmp")
 
What happens when you deploy your app to another system? You're hardly going to have a Visual Studio Projects folder on every system. There are two realistic options.

1. Place the image file in the same folder as your EXE and use Application.StartupPath to get the path.
2. Embed your image a resource in your EXE and extract it when required.
 
Back
Top