|
-
Apr 25th, 2004, 10:38 AM
#1
Thread Starter
Lively Member
Thingamajig in the whatchamacallit.
Yeah, this is going to sound really stupid, but what's the best way to save program settings in order to reload on the next launch? Like when a user selects a folder to download to, should I stash that in the registry or something and when is the best time to load that on the next program startup? On the form load or main or what?
Take it easy on me I've been up all night.
-
Apr 25th, 2004, 12:26 PM
#2
Hyperactive Member
Dear KT3, I'm working just around the same problem. I'm a beginner and think many friends of this forum will give you better ideas, anyway, this is mine:
When setting form is loaded, it tries to read a particular file:
"C:\AEASRL\GIN\INIGIN.TXT"
If an error occurs, an Array is filled with default values, otherwise it is filled with the values found in the file.
Obviously. there is code to manipulate these values.
Then, when the form is closing, the user can choose if save or not the last changes, if any
The File is a text file written by a STREAMWRITER and read by a STREAMREADER
Any row contains a paramether name and value.
The first 30 characters (fixed lenght) is for the name. If it's shorter, I use padright method to reach the lenght with the right number of spaces.
This is the basic idea. I'm testing and debugging it just now. It seems work fine, but I need more time to sentence it!
Excuse my english. Hope to be useful.
Live long and prosper (Mr. Spock)
-
Apr 25th, 2004, 02:22 PM
#3
Frenzied Member
You can write the settings to the registry.
Then read them from it everytime the application is launched.
Being educated does not make you intelligent.
Need a weekend getaway??? Come Visit
-
Apr 25th, 2004, 02:35 PM
#4
Thread Starter
Lively Member
Alright! I think I might just do that.
...
So, umm.. How do I do that? I mean, I'm pretty sure I can figure out how to access the registry on my own, but I need to get the path to the user's desktop to enable that as the default download location unless an alternate selected location is found by reading the registry.
-
Apr 25th, 2004, 02:37 PM
#5
Thread Starter
Lively Member
Alex, thanks for the help, man, but I think if you were going to just do that, wouldn't you be better off using the application configuration file? Either way, I'd much rather use the registry, because it makes it more transparent to the user. For this application anyway. For tools and stuff I might want to use an .ini file because I know people like me love to be able to move those tools to other PCs and keep all their settings without backing up the damn registry keys.
Last edited by Kt3; Apr 25th, 2004 at 02:41 PM.
-
Apr 25th, 2004, 02:56 PM
#6
Hyperactive Member
Dear KT3, thanks for the suggest, I will have a look to the application configuration file. At the moment I don't know which kind of animal it was
To write in the register is perhaps the best way for someone advanced.
Anyway I prefer don't put my inexpert hands in shared files. I don't want to risk to do something wrong in files that other applications need. I want to be sure, like was in the ancient DOS applications, that you can cancel all traces of my applications only quickly deleting one or two folders. But that is only my personal point of view.
Live long and prosper (Mr. Spock)
-
Apr 26th, 2004, 02:36 AM
#7
Registry? Yuck how late 90's is that? 
What I always do is write a serializable class / structure that contains all settings for the app and then save / load a binary file. Fast, efficient, incomprehensible to (my) fellow programmers thus ensuring job security
I don't live here any more.
-
Apr 26th, 2004, 07:56 AM
#8
Thread Starter
Lively Member
Originally posted by wossname
Registry? Yuck how late 90's is that? 
What I always do is write a serializable class / structure that contains all settings for the app and then save / load a binary file. Fast, efficient, incomprehensible to (my) fellow programmers thus ensuring job security
Oh you ba*tard!
-
Apr 26th, 2004, 09:34 AM
#9
Thread Starter
Lively Member
Ok, I did it. I made a serializable class to hold all configuration information. Aren't you proud?
Documenting it for those that care. This is the serializable class that contains a Download property which sets or returns a string containing the user specified download folder path.
VB Code:
<Serializable()> _
Public Class Configuration
Private _SelectedPath As String
Public Property Download() As String
Get
Return _SelectedPath
End Get
Set(ByVal Value As String)
_SelectedPath = Value
End Set
End Property
End Class
Here is how it is access on startup:
VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mainBar.Visible = False
Try
Dim fs As New FileStream("snlv2.dat", FileMode.Open)
Dim r As New StreamReader(fs)
Dim formatter As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
config = CType(formatter.Deserialize(fs), Configuration)
saveTo.SelectedPath = config.Download
r.Close()
fs.Close()
Catch ex As System.IO.FileNotFoundException
End Try
End Sub
In my application, when a user clicks this button:
VB Code:
Private Sub configDownload_Click(ByVal sender As System.Object, ByVal e As C1.Win.C1Command.ClickEventArgs) Handles configDownload.Click
SaveDownloadLocation()
End Sub
It calls the SaveDownloadLocation routine, which runs this code. saveTo is an instance of the System.Windows.Forms.FolderBrowserDialog object.
VB Code:
Private Sub SaveDownloadLocation()
saveTo.ShowDialog()
config.Download = saveTo.SelectedPath
Dim formatter As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim fs As New FileStream("snlv2.dat", FileMode.Create)
formatter.Serialize(fs, config)
fs.Close()
End Sub
In my software I have a listview that has a list of downloadable files. On a doubleclick event, besides trapping the item under the mouse in the listview, it also checks to see if saveTo.SelectedPath is an empty string. If so, it calls the SaveDownloadLocation() method to allow the user to specify a download location first, in case that hasn't been done. Of course, if the user has previously specified a download location and wishes to change the location, there is a button on the toolbar which will do this, the configDownload command, and its event is handled as shown above.
I also caught an exception in my download method so that if the selected path is invalid, i.e. the folder doesn't exist, it calls the SaveDownloadLocation() routine.
And, you're probably thinking, "who's this wise guy who thinks he knows it all? Strolling in here onto our forums documenting some piece of crap application he wrote like we care? "
I'm KT, 20 years old, and I've only been learning to code in VB.NET and some C#, PHP, Perl and Python for only one month. I don't have any other programming experience besides that. 
Peace,
KT
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|