Results 1 to 20 of 20

Thread: Wrong Directory Path at startup

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Wrong Directory Path at startup

    Hello,
    I'm using this code to make my app. run at startup if a CheckBox is checked

    Code:
       Private Sub CheckBox5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox5.CheckedChanged
            If CheckBox5.Checked = True Then
                CheckBox6.Checked = False
                CheckBox6.Enabled = False
                Dim key As Microsoft.Win32.RegistryKey
                key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True)
                key.SetValue("To-Do List", Application.ExecutablePath)
                My.Settings.DefaultCheck5 = True
            ElseIf CheckBox5.Checked = False Then
                CheckBox6.Enabled = True
                Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).DeleteValue("To-Do List", False)
                My.Settings.DefaultCheck5 = False
            End If
        End Sub
    However, when the app. starts it loads some text to a ListBox. But, when the app. run at startup it creates a directory in:

    C:\Documents and Settings\%userprofile%\%app. folder%

    and its supposed to load the text from

    Directory.GetCurrentDirectory.ToString & "\My Program\textfile.txt"

    This is my FormLoad code:

    Code:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ListBox1.Items.Clear()
            'Sets the CurrentUser to an empty string
            My.Settings.CurrentUser = ""
            'Gets the curent directory of the application
            My.Settings.CurrentDirectory = Directory.GetCurrentDirectory.ToString
            'Checks to see if it's the first run of the program
            If My.Settings.FirstRun = True Then
                UserINFO.ShowDialog()
                My.Settings.FirstRun = False
            ElseIf My.Settings.FirstRun = False Then
                'Checks if the Settings folder exists
                If Directory.Exists(My.Settings.CurrentDirectory & "\Settings\Usernames") = False Then
                    Directory.CreateDirectory(My.Settings.CurrentDirectory & "\Settings\Usernames")
                End If
            End If
            'Handles the ToolTip delay
            Me.ToolTip1.ReshowDelay = 10
            Me.ToolTip1.InitialDelay = 1000
            'Handles fade-in effects
            If My.Settings.DisableFadeIn = True Then
                Timer1.Enabled = False
                Me.Opacity = 100
                ManageUsers.Timer1.Enabled = False
                ManageUsers.Opacity = 100
            ElseIf My.Settings.DisableFadeIn = False Then
                Timer1.Enabled = True
                ManageUsers.Timer1.Enabled = True
            End If
            'Settings from Customize form
            With Me
                .ListBox1.ForeColor = My.Settings.DefaultColor
                .FormBorderStyle = My.Settings.DefaultCustomBorder
                .Button1.Visible = My.Settings.DefaultButton1
                .Button2.Visible = My.Settings.DefaultButton2
                .Button3.Visible = My.Settings.DefaultButton3
                .Size = My.Settings.DefaultSize
                .ShowInTaskbar = My.Settings.ShowInTaskbar
            End With
            With Customize
                .CheckBox1.Checked = My.Settings.DefaultCheck1
                .CheckBox2.Checked = My.Settings.DefaultCheck2
                .CheckBox3.Checked = My.Settings.DefaultCheck3
                .CheckBox4.Checked = My.Settings.DefaultCheck4
                .CheckBox5.Checked = My.Settings.DefaultCheck5
                .CheckBox6.Checked = My.Settings.DefaultCheck6
                .CheckBox7.Checked = My.Settings.DefaultCheck7
            End With
            PrintSettings.Enabled = My.Settings.DefaultPrinterSettings
            'Settings end here
            ManageUsers.ShowDialog()
            TextBox1.Focus()
        End Sub
    Last edited by tassa; Mar 25th, 2009 at 10:17 PM. Reason: Had posted the wrong code...
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Wrong Directory Path at startup

    Is this a ClickOnce-deployed app by any chance?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Wrong Directory Path at startup

    ClickOnce-deployed app what do you mean by that?
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Wrong Directory Path at startup

    I mean did you deploy the app using ClickOnce, i.e. did you publish your project to create a ClickOnce installer?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Wrong Directory Path at startup

    No, i haven't published my app.
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Wrong Directory Path at startup

    Why are you storing a current directory path in your settings? What is that supposed to represent?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Wrong Directory Path at startup

    I'm a little confused by your code so I'm just going to ignore it and tell you what you SHOULD do:
    vb.net Code:
    1. Imports Microsoft.Win32
    2.  
    3. Public Class Form1
    4.  
    5.     Private Sub Form1_Load(ByVal sender As Object, _
    6.                            ByVal e As EventArgs) Handles MyBase.Load
    7.         'If GetValue returns Nothing then there is no value with that name, so the app is not being run at startup.
    8.         Me.runAtStartupCheck.Checked = (Registry.GetValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", _
    9.                                                           Application.ProductName, _
    10.                                                           Nothing) IsNot Nothing)
    11.  
    12.     End Sub
    13.  
    14.     Private Sub runAtStartupCheck_CheckedChanged(ByVal sender As Object, _
    15.                                                  ByVal e As EventArgs) Handles runAtStartupCheck.CheckedChanged
    16.         If Me.runAtStartupCheck.Checked Then
    17.             'Add the executable's path against the application's name.
    18.             Registry.SetValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", _
    19.                               Application.ProductName, _
    20.                               Application.ExecutablePath)
    21.         Else
    22.             'Remove the application's name.
    23.             Using key = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", _
    24.                                                         True)
    25.                 key.DeleteValue(Application.ProductName)
    26.             End Using
    27.         End If
    28.     End Sub
    29.  
    30. End Class
    You will obviously(?) need to set the Product Name in the project properties.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Wrong Directory Path at startup

    Quote Originally Posted by jmcilhinney View Post
    Code:
        Private Sub Form1_Load(ByVal sender As Object, _
                               ByVal e As EventArgs) Handles MyBase.Load
            'If GetValue returns Nothing then there is no value with that name, so the app is not being run at startup.
            Me.runAtStartupCheck.Checked = (Registry.GetValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", _
                                                              Application.ProductName, _
                                                              Nothing) IsNot Nothing)
    
        End Sub
    Yes, I know the code is confusing, trying to make it easier to understand... What is the quoted code for?
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Wrong Directory Path at startup

    Quote Originally Posted by tassa View Post
    Yes, I know the code is confusing, trying to make it easier to understand... What is the quoted code for?
    It's to set the state of the CheckBox when the app starts up. You shouldn't be storing anything in My.Settings for this because what's in My.Settings doesn't matter. Only what's in the Registry matters. If the user goes into the Registry and deletes your value by hand then the next time your app starts your box will be checked, even though the app is NOT being run at startup. As such you're giving the user incorrect information.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Wrong Directory Path at startup

    Ok ok. By that, I suppose that I can change the value of 6 checkboxes by the values stored in the registry?
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Wrong Directory Path at startup

    Quote Originally Posted by tassa View Post
    Ok ok. By that, I suppose that I can change the value of 6 checkboxes by the values stored in the registry?
    Absolutely. That said, note that My.Settings is supposed to be a substitute for the Registry in many situations. You should avoid storing app-specific configuration data and the like in the Registry and use My.Settings for that. Data that needs to be read by the OS or other apps, like these Run values, must still be stored in a common location, like the Registry.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Wrong Directory Path at startup

    Ok. Now I have one question:

    1-How can I store more than 1 value in the registry?
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Wrong Directory Path at startup

    Quote Originally Posted by tassa View Post
    Ok. Now I have one question:

    1-How can I store more than 1 value in the registry?
    Do you mean like under the name MyValue you would have multiple sub-values, or do you just mean having MyValue1 with a value, MyValue2 with another value and MyValue3 with yet another value?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Wrong Directory Path at startup

    Well, I think that adding sub-values makes things more comprehensible and isn't doing that better?
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Wrong Directory Path at startup

    Quote Originally Posted by tassa View Post
    Well, I think that adding sub-values makes things more comprehensible and isn't doing that better?
    That would depend. For instance, if you wanted to save a list of people's names then a single value containing multiple sub-values would be the way to go, because it all represents a single entity. If you wanted to save, say, a single person's name and their address though, that would be better done with two separate values, because they represent two separate, if related, entities.

    What exactly does this information represent that you want to save?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Wrong Directory Path at startup

    Well, i have a form, say Form2, that has 6 checkboxes, each checkbox represent an integer... For instance...

    CheckBox1, disables the form border
    CheckBox2, disables another form (this value is a boolean)
    CheckBox4, makes form's buttons invisible
    CheckBox5, makes the app. run at startup
    CheckBox6, enables a horizontal track bar to change the form's opacity..

    How would it be better to save this values?
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  17. #17
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Wrong Directory Path at startup

    As I said earlier, app-specific configuration should be stored in My.Settings, while data that must be read by the OS and/or other apps too should be stored in a common repository, i.e. the Registry. In your case, whether or not the application is run at startup is the only value that is accessed from outside the application, so it's the only value that should be stored in the Registry. Everything else is meaningless to all but your app, so it should be stored local to your app, i.e. in My.Settings.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  18. #18

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Wrong Directory Path at startup

    OK. Thanks. But it still doesnt explain why it doesnt load the text from the current directory, but instead from C:\Documents and Settings\%userprofile%\%app. folder%
    Last edited by tassa; Mar 25th, 2009 at 11:43 PM. Reason: I think i can manage to solve that with the code you posted
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  19. #19
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Wrong Directory Path at startup

    As I said earlier, I have no idea what My.Settings.CurrentDirectory is for. You're setting it to the app's current working directory when you start up every time so what's the point? I also don't see anywhere that text is being loaded into a ListBox.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  20. #20

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Wrong Directory Path at startup

    Let me check that up. I'll come back with more details. Thanks for you help =D
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width