Click here to Skip to main content
16,015,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can someone help me with my problem?
I already have a program that saves my data in the location I declared in the program (D:\Data Save\).
What I want now is I choose the location using the folder browser dialog that pastes the location in a textbox. This one I can browse already and paste the location in the text box. But the problem is when I change the (D:\Data Save\) into my textbox the data is not saving.
I'm working on this for many days already. So I need help from the expert.

In my current program, I declare the D:\Data Save\ for the location, but I want to change it depending on the location I choose in my text label using the folder browser dialog.
Thank you in advance.

What I have tried:

VB
Using sw As StreamWriter = File.CreateText("D:\Data Save\" + DateTime.Now.ToString("ddMMyyyy_hhmmss") & "_Line" & Form1.LineTxt.Text + ".csv")
    Dim dgvColumnNames As List(Of String) = Form1.DataGridView1.Columns.
            Cast(Of DataGridViewColumn).ToList().
            Select(Function(c) c.Name).ToList()

    sw.WriteLine(String.Join(",", dgvColumnNames))

    For Each row As DataGridViewRow In Form1.DataGridView1.Rows

        Dim rowData As New List(Of String)
        For Each column As DataGridViewColumn In Form1.DataGridView1.Columns
            rowData.Add(Convert.ToString(row.Cells(column.Name).Value))
        Next
        sw.WriteLine(String.Join(",", rowData))
        sw.Close()
    Next

End Using
Posted
Updated 9-Aug-23 20:08pm
v2
Comments
Marlon Rivera 10-Aug-23 3:39am    
Thank you so much. It works 100%.

1 solution

That code uses a fixed path - it always saves under the "D:\Data Save\" folder, but includes the data from your textbox as part of the file name.
Which means that if textbox contains "E:\MyFolder", the final path string you try to use will be something like this:
Path
D:\Data Save\10082023_070135_LineE:\MyFolder.csv
If you want the folder location to be in the textbox, then you need to replace the folder part with the textbox content:
VB
Dim pathToFile as String = Form1.LineTxt.Text + DateTime.Now.ToString("ddMMyyyy_hhmmss") & "_Line.csv"
Using sw As StreamWriter = File.CreateText(pathToFile)
Or better:
VB
Dim pathToFile as String = Path.Combine(Form1.LineTxt.Text, DateTime.Now.ToString("ddMMyyyy_hhmmss") & "_Line.csv"
Using sw As StreamWriter = File.CreateText(pathToFile)
Because it adds "/" separators as needed.

BTW: it's a much better idea to use ISO date format:
VB
... DateTime.Now.ToString("yyyyMMdd_hhmmss") ...
as the resulting file names are easily sorted into ascending or descending order (and there is less confusion if teh system is set to US date format).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900