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
Re: Wrong Directory Path at startup
Is this a ClickOnce-deployed app by any chance?
Re: Wrong Directory Path at startup
ClickOnce-deployed app what do you mean by that?
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?
Re: Wrong Directory Path at startup
No, i haven't published my app.
Re: Wrong Directory Path at startup
Why are you storing a current directory path in your settings? What is that supposed to represent?
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:
Imports Microsoft.Win32
Public Class Form1
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
Private Sub runAtStartupCheck_CheckedChanged(ByVal sender As Object, _
ByVal e As EventArgs) Handles runAtStartupCheck.CheckedChanged
If Me.runAtStartupCheck.Checked Then
'Add the executable's path against the application's name.
Registry.SetValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", _
Application.ProductName, _
Application.ExecutablePath)
Else
'Remove the application's name.
Using key = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", _
True)
key.DeleteValue(Application.ProductName)
End Using
End If
End Sub
End Class
You will obviously(?) need to set the Product Name in the project properties.
Re: Wrong Directory Path at startup
Quote:
Originally Posted by
jmcilhinney
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?:confused:
Re: Wrong Directory Path at startup
Quote:
Originally Posted by
tassa
Yes, I know the code is confusing, trying to make it easier to understand... What is the quoted code for?:confused:
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.
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?
Re: Wrong Directory Path at startup
Quote:
Originally Posted by
tassa
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.
Re: Wrong Directory Path at startup
Ok. Now I have one question:
1-How can I store more than 1 value in the registry?
Re: Wrong Directory Path at startup
Quote:
Originally Posted by
tassa
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?
Re: Wrong Directory Path at startup
Well, I think that adding sub-values makes things more comprehensible and isn't doing that better?
Re: Wrong Directory Path at startup
Quote:
Originally Posted by
tassa
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?
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?
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.
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%
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.
Re: Wrong Directory Path at startup
Let me check that up. I'll come back with more details. Thanks for you help =D