|
-
Mar 22nd, 2013, 12:37 PM
#1
Thread Starter
Junior Member
[RESOLVED] Getting values between brackets where anothe value exists
Hey Everyone
I have a program that is designed to allow users to buy and sell models (as a small computing project). The user has to register an account which is stored in a database separately. They must Login to be able to edit their account information. I have a form called UserSettings. This contains the text boxes and drop down boxes with profile information (e.g. name, email, first and last name, etc). However at the bottom of this form I have two text boxes, one which is called "Default Model Download Location" and the other is called "Default model upload Location". In my application I have a routine that creates a file (called "mus", it stands for modeller user settings). Certain user settings are stored in here (like a ini file).
Example structure of a *.mus file:
Code:
ModelDownload_Directory=[C:\Modeller\Models\Download]
ModelUpload_Directory=[C:\Modeller\Models\Upload]
I want to be able to create a routine that goes like this:
Code:
txtModelDownloadLocation.text = value WHERE ModelDownload_Directory=
txtModelUpload_Directory.text = value WHERE ModelUpload_Directory=
I was thinking this could be possible if done with regex, but I've spend all day trying to figure it out and getting no where.
Thank you for reading and I would appreciate any help!
-
Mar 22nd, 2013, 12:57 PM
#2
Re: Getting values between brackets where anothe value exists
Hello,
I would suggest looking at storing this information under My.Settings. You can bind a single setting say to a TextBox.
Basically you end up with little code
Code:
Private Sub cmdSelectDownLoadPath_Click(
ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles cmdSelectDownLoadPath.Click
If Not String.IsNullOrWhiteSpace(My.Settings.ModelDownloadPath) Then
FolderBrowserDialog1.SelectedPath = My.Settings.ModelDownloadPath
End If
If FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
txtDownloadPath.Text = FolderBrowserDialog1.SelectedPath
End If
End Sub
You set the binding for say a TextBox under the TextBox properties --> ApplicationSettings, Property Binding
-
Mar 22nd, 2013, 01:02 PM
#3
Thread Starter
Junior Member
Re: Getting values between brackets where anothe value exists
Thank you for the quick response, I will try this out. The only problem with this (which my fault because I forgot to mention) is that the data that is stored in the file is encrypted when the application isn't using the data and temporarily decrypted when the file needs to be written or read (in memory). The reason I was using a file is so that I can store different bits of local user settings (including features I would like to add in the future which may not be linked to just text boxes, but also other components).
-
Mar 22nd, 2013, 01:09 PM
#4
Re: Getting values between brackets where anothe value exists
All this means is you do not create the settings via the method I spoke of but instead under Project properties and not use data binding but instead manually set text property of a TextBox.
So say after the user selects a path you would encrypt the text and then assign it to the settings. When reading the setting you decrypt then assign it to the Textbox.
-
Mar 22nd, 2013, 01:09 PM
#5
Thread Starter
Junior Member
Re: Getting values between brackets where anothe value exists
Think I'm going to try and create a local XML file that is encrypted and decrypted like I have done with the *.mus file, that way I should be the " ModelDownload_Directory= " part as an element and the " C:\Modeller\Models\Download " as a value. I've never really used XML before (that's why I was trying to avoid it before)
-
Mar 22nd, 2013, 01:11 PM
#6
Re: Getting values between brackets where anothe value exists
Using My.Settings is perfered method for stuff like this as the mechanics are handled for you unlike XML. You could read the xml via DataSet.ReadXml but I believe the My.Settings method is better and easier to work with
-
Mar 22nd, 2013, 01:18 PM
#7
Re: Getting values between brackets where anothe value exists
This would be the regex:
Short explination:
- \[ - Literally matches a "["
- (.*?) - the 'everything' inbetween
- \] - Literally matches a "]"
Putting it into vb.net syntax:
Code:
Dim str As String = "ModelDownload_Directory=[C:\Modeller\Models\Download]" & Environment.NewLine & "ModelUpload_Directory=[C:\Modeller\Models\Upload]"
Dim reg As New Regex("\[(.*?)\]")
Console.WriteLine("Model Download: " & reg.Matches(str).Item(0).ToString.Replace("[", "").Replace("]", ""))
Console.WriteLine("Model Upload: " & reg.Matches(str).Item(1).ToString.Replace("[", "").Replace("]", ""))
Console.ReadLine()
My regex is a little off because I use replace to get rid of the brackets... I'm sure someone else could give it to you where to didn't have to add the replace.
Edit -
Wow I sat in this post a while, stepped away from my computer, finished it, and then when I posted it there was like 8 other post already!
-
Mar 22nd, 2013, 04:33 PM
#8
Thread Starter
Junior Member
Re: Getting values between brackets where anothe value exists
Thank you, that looks like a good and efficient way doing it. I tried to create a regex pattern that would do it but couldn't think of one (I will try this out later). I managed to sort the problem out in a weird but useful way (I think I might fix and update all of the code to get rid of bugs or inconsistent programming styles). This is the code I am currently using:
Code:
Public Sub UserSettingConfigurator()
Dim ToRead As String = UserSettings_MDS.MusContent_UserSettings
MsgBox(ToRead)
'Read all lines in the Usersettings in private memory
Try
Dim i As Integer = ToRead.IndexOf("@1[")
Dim f As String = ToRead.Substring(i + 3, ToRead.IndexOf("]1@", i + 1) - i - 3)
UserSettings_MDS.ModelDownloadPath = f
i = ToRead.IndexOf("@2[")
f = ToRead.Substring(i + 3, ToRead.IndexOf("]2@", i + 1) - i - 3)
UserSettings_MDS.ModelUploadPath = f
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Mus File:
Code:
ModelDownload_Directory=@1[C:\Modeller\Models\Download\]1@
ModelUpload_Directory=@2[C:\Modeller\Models\Upload\]2@
Because I couldn't figure out the regex I tried to think of doing it in another manner. By giving the values a unique container
Code:
@1[ Contents goes here ]1@
, then I could read to the start of the unique container and the end of the unique container, and then delete 3 characters at the beginning and end of the container to leave the value behind. (E.g.
Code:
@1[C:\Modeller\Models\Download\]1@
->
Code:
C:\Modeller\Models\Download\
).
Thanks for your suggestions, and I will be sure to try them out later as I would like to see there results. However my realisation is what currently best fits with my project, and as I'm on a short deadline it's the only thing I can really use.
Thanks again guys!
-
Mar 23rd, 2013, 07:01 AM
#9
Re: Getting values between brackets where anothe value exists
In short you are writing code that there are better methods in the Framework already. Besides what I already suggesteted (dataset, My.Settings) there are methods to serialize classes. All can have encryption. Serializing a class with encryption first off can only be properly read using code from the same namespace that created it so there is double security. Of course an expert hacker over time can break anything, depends how deep down the rabbit hole you are willing to go.
Tags for this Thread
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
|