0

I have a GUI where the user is capable of logging data that comes in from the serial port. I have it set up where I can specify the filename and path that the streamwriter uses in the code, but I would like to have the user select their own path and filename. I was thinking maybe something with the SaveFileDialogue, but I couldn't figure anything out. The file is simply being written as a .csv as well. Open to any suggestions!

My code for declaring the streamwriter is:

    Public Class ReadMeasuredValues
Dim sw as StreamWriter

    Private Sub startread_Click(sender As System.Object, e As System.EventArgs) Handles StartRead.Click
            sw = New StreamWriter(savefiledialog1.filename, False)

    If CheckBox1.Checked = True Then
        sw.WriteLine("Date, Time, Ch 1, Ch 2, Ch 3, Ch 4, Ch 5, Ch 6, Ch 7, Ch 8, Ch 9, Ch 10, Ch 11, Ch 12, Ch 13, Ch 14, Ch 15, Ch 16, Ch 17, Ch 18, Ch 19, Ch 20, Ch 21, Ch 22, Ch 23, Ch 24, Ch 25, Ch 26, Ch 27, Ch 28, Ch 29, Ch 30, Ch 31, Ch 32")
    End If
    End Sub

Code for launching the SaveFileDialog is

    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
    saveFileDialog1.Filter = "CSV|*.csv"
    saveFileDialog1.RestoreDirectory = True
    If CheckBox1.Checked = True Then
    savefiledialog1.showDialog()
                End If
    End Sub
  • [**How to: Save Files Using the SaveFileDialog Component**](https://msdn.microsoft.com/en-us/library/sfezx97z(v=vs.110).aspx). – Visual Vincent Aug 22 '16 at 14:22
  • Please edit your question via the `Edit` button instead. It's almost impossible to read code in the comments. – Visual Vincent Aug 22 '16 at 16:05
  • my apologies. The code is in my edited question – Austin Parker Aug 22 '16 at 16:42
  • What happened to your `SaveFileDialog` attempt? What you need to do is: **1)** Open the dialog via `ShowDialog()` and check the return value of it (`OK` = user chose to save the file). **2)** Initialize the `StreamWriter` passing the SaveFileDialog's `FileName` as file path. **3)** Write. – Visual Vincent Aug 22 '16 at 16:55
  • I've added the code for launching the dialog. I'd like to launch the dialog when the check box is checked and write one line to the streamwriter on a button press and then a set of lines on a timer interval – Austin Parker Aug 22 '16 at 17:04
  • You are declaring a new `StreamWriter` in the check box. Move it out to _class level_ (outside any `Sub` or `Function`) so that you can access it from within your entire form instead. – Visual Vincent Aug 22 '16 at 17:12
  • If I do it that way, I can't have the filepath be `savedialog1.filename` because the filename will be blank and I won't be able to launch my form – Austin Parker Aug 22 '16 at 17:22
  • Yes you can, if you wait with initializing the `StreamWriter` until you've closed the SFD. – Visual Vincent Aug 22 '16 at 17:32
  • Now how would I go about doing that? – Austin Parker Aug 23 '16 at 14:04
  • My declaration is `dim sw as new StreamWriter(savefiledialog1.filename, false)` – Austin Parker Aug 23 '16 at 14:15
  • You just wait with initializing the `StreamWriter` until you've closed the SFD. For example, class level: `Dim sw As StreamWriter` - after closing the SFD: `sw = New StreamWriter(SaveFileDialog1.FileName, False)`. – Visual Vincent Aug 23 '16 at 16:36
  • It breaks at this line `sw = New StreamWriter(savefiledialog1.FileName, False)` and I get the error that it's being used by another process – Austin Parker Aug 23 '16 at 16:55
  • The error tells you exactly what's going on. Are you trying to overwrite a file? – Visual Vincent Aug 23 '16 at 18:38
  • If it exists, it should be overwritten, if not, it should be created. But I don't understand what other process could be using it – Austin Parker Aug 23 '16 at 18:41
  • Can you modify it manually? – Visual Vincent Aug 23 '16 at 19:08
  • Yes, I can. The file will be created if it doesn't exist, as it should, but nothing is ever written due to the error – Austin Parker Aug 23 '16 at 19:09
  • Can you update the question again with your latest code? Otherwise you can use [Process Explorer](https://technet.microsoft.com/en-us/sysinternals/processexplorer.aspx) to find which processes locks your file. Open Process Explorer and press CTRL + F, then type in the name of the file and it should display the process using it. – Visual Vincent Aug 23 '16 at 19:13
  • I don't see anything that should be a problem. Try Process Explorer and see what locks the file (also make sure it isn't your own app). – Visual Vincent Aug 23 '16 at 19:25
  • I also am using the StreamWriter to write to the same file in two different subs if that matters at all. Once on a button press and repeatedly on a timer tick – Austin Parker Aug 23 '16 at 19:26
  • Reusing the very same `StreamWriter` doesn't matter, only opening two different ones and attaching them to the same file does. – Visual Vincent Aug 23 '16 at 19:28
  • Should I use the same line `sw = New StreamWriter(savefiledialog1.filename, False)` in both subs? – Austin Parker Aug 23 '16 at 19:31
  • No :). Once you've called `New` you have initialized an instance of the `StreamWriter` which you can now use until you close it (or until it goes out of scope, but it won't do that in your class). – Visual Vincent Aug 23 '16 at 19:33
  • Calling `New StreamWriter` two times will result in creating two different `StreamWriter`s. The first one is replaced by the second one, but that doesn't mean that the first one has released its lock to the file. – Visual Vincent Aug 23 '16 at 19:36

0 Answers0