0

OK, so I am having some trouble with writing to a text file, retrieving values from My.Settings. The code is below. The problem is that when I check the text file, it only has the first line (which is just a simple string in the code).

Dim settingsfile As String = Directory.GetCurrentDirectory & "\RapidSend_Settings.txt"
Dim objWriter As New System.IO.StreamWriter(settingsfile, False)
objWriter.WriteLine("This is the introductory line")
For Each strEmail As String In My.Settings.User_Emails 'Specialized.StringCollection
    objWriter.WriteLine("Email " & strEmail)
Next
For Each strHostName As String In My.Settings.User_HostName ' Specialized.StringCollection
    objWriter.WriteLine("Host " & strHostName)
Next
objWriter.WriteLine("End Of File")
objWriter.Close()

This is what the text file looks like after the code:

 This is the introductory line
 End Of File

My question is does anyone know what the problem is, or is there another way to do this. BTW, all the My.Settings are Specialized.StringCollection, so they contain lots of strings.

Centaurian
  • 277
  • 1
  • 3
  • 11
  • set a breakpoint at the start and look at how many items there are in the collection. the code should work if there are items there. However, when deployed, you likely will not have write access to `GetCurrentDirectory` and and exception will result. Better to write to a `Users...` folder. – Ňɏssa Pøngjǣrdenlarp Jul 03 '14 at 15:39
  • What do you mean breakpoint? I tried `If My.Settings.User_Emails.Count = 0 Then MsgBox("Empty Collection") Else [WRITE TO FILE] End If` And it still doesn't work. And I do have access. Thanks – Centaurian Jul 03 '14 at 15:43
  • 1
    http://msdn.microsoft.com/en-us/library/vstudio/5557y8b4(v=vs.110).aspx they are a big, big big part of debugging code. ...and you probably wont have access to `CurrentDirectory` **when deployed** – Ňɏssa Pøngjǣrdenlarp Jul 03 '14 at 15:46
  • I don't get any errors. I added msgboxes to tell me where the code is, and it runs through everything. I even added 'MsgBox(strEmail)' in the For Loop and it shows me each string, but it just doesn't write to the text file. – Centaurian Jul 03 '14 at 15:53
  • using the collection in MySettings is probably not ideal either since you are not using it as intended. there can be issues with versions and the default versus current contents. Use `emails As New List(Of String)` instead. you add and iterate it the same as that collection, but it will be **your** container, not that of VB/VS/NET – Ňɏssa Pøngjǣrdenlarp Jul 03 '14 at 16:08
  • I tried adding breakpoints, and I am getting the values for the variables how they should be, but it just does't write to the text file. I think I will just use a variable to hold the values. How do I make them global (I mean access from all forms)? – Centaurian Jul 03 '14 at 16:27

1 Answers1

1

Since you are no longer using Settings the way it is intended, you are better off creating your own container and managing it yourself:

  • Add a module to your app if there isnt one
  • Add Friend settingsList As New List(Of String) to it

This will create a global collection of strings you can add your data to. It can be Public or Friend. There are other ways to do this, but this is simplest. Create one for the hosts too.

Adding to them is simple:

settingsList.Add(some_STRING_variable)

Then to save in the App shutdown:

' get file name to hold the data
Dim file As String
file = System.IO.Path.Combine(Environment.GetFolderPath( _
       Environment.SpecialFolder.ApplicationData),  "myproduct", "settings.txt")
' result will be Users\Appdata.... where data is supposed to go

' open a stream
Using fs As New IO.StreamWriter(file, IO.FileMode.CreateNew)
    Dim n As Integer = 1
    ' loop thru List contents
    For Each s As String In settingsList
        ' ToDo: edit format to fit your needs
        fs.WriteLine(String.Format("{0} = {1}", n.ToString, s))
        n += 1
    Next

End Using         ' close and properly dispose of stream
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178