Save to a file

K

Kelly

Ok, I need a little help. :) So far, I've written a program that will allow
a user to browse for a file (via a dialog box) and open the selected file. I
need to make some slight modifications to that and have the user's data
(entered into a textbox) saved to a selected file (from the dialog
box)...here's what I have done so far. I was hoping that someone could steer
me in the right direction. Thanks!

Dim myFileDlog As New OpenFileDialog

myFileDlog.InitialDirectory = "C:\"

myFileDlog.Filter = "Text Files |*.txt|All files | *.*"

If myFileDlog.ShowDialog() = DialogResult.OK Then

Dim sFileName As String = myFileDlog.FileName

Dim sFileExtension As String = (sFileName.Substring(sFileName.Length - 4))

Select Case sFileExtension.ToUpper

Case ".TXT"

Shell("notepad.exe " & sFileName, AppWinStyle.MaximizedFocus)

Case ".DOC"

Shell("path to word.exe goes here" & sFileName, AppWinStyle.MaximizedFocus)

Case ".XLS"

Shell("path to excel.exe goes here " & sFileName,
AppWinStyle.MaximizedFocus)

Case Else

MsgBox("not a valid file extension")

End Select

Else

MsgBox("User pressed Cancel.")

End If
 
H

Herfried K. Wagner [MVP]

Kelly,

* "Kelly said:
Ok, I need a little help. :) So far, I've written a program that will allow
a user to browse for a file (via a dialog box) and open the selected file. I
need to make some slight modifications to that and have the user's data
(entered into a textbox) saved to a selected file (from the dialog
box)...here's what I have done so far. I was hoping that someone could steer
me in the right direction. Thanks!

Use the 'SaveFileDialog' instead of the 'OpenFileDialog'.
Dim myFileDlog As New OpenFileDialog

myFileDlog.InitialDirectory = "C:\"

myFileDlog.Filter = "Text Files |*.txt|All files | *.*"

Remove the spaces in front of and behind the pipe characters ("|").
If myFileDlog.ShowDialog() = DialogResult.OK Then

Dim sFileName As String = myFileDlog.FileName

Dim sFileExtension As String = (sFileName.Substring(sFileName.Length - 4))

Use 'System.IO.Path.GetExtension' instead of 'Substring'.
Select Case sFileExtension.ToUpper

Case ".TXT"

Shell("notepad.exe " & sFileName, AppWinStyle.MaximizedFocus)

Case ".DOC"

Shell("path to word.exe goes here" & sFileName, AppWinStyle.MaximizedFocus)

Case ".XLS"

Shell("path to excel.exe goes here " & sFileName,
AppWinStyle.MaximizedFocus)

Case Else

MsgBox("not a valid file extension")

End Select

Else

MsgBox("User pressed Cancel.")

End If

\\\
myFileDlog.Dispose()
///

I am not sure what your question is a about. Do you want to know how to
write data to a file? Take a look at the methods in
'Microsoft.VisualBasic.FileSystem' ('FileOpen', 'WriteLine',
'FileClose', ...) and in the 'System.IO' namespace ('StreamWriter',
....).
 
K

Kelly

Thanks for the tips! Yes, I'm trying to write the data the user enters into
a file.

I looked at the Write and WriteLine methods, but didn't quite understand
where the FileNumber comes in and whether or not I needed to create one. I'm
pretty new at .NET, so this is a big learning experience. Quite different
than VB6!

Thanks again!
 
H

Herfried K. Wagner [MVP]

Kelly,

* "Kelly said:
Thanks for the tips! Yes, I'm trying to write the data the user enters into
a file.

I looked at the Write and WriteLine methods, but didn't quite understand
where the FileNumber comes in and whether or not I needed to create one. I'm
pretty new at .NET, so this is a big learning experience. Quite different
than VB6!

\\\
Dim f As Integer = FreeFile()
FileOpen(f, "C:\foo.txt", OpenMode.Append)
PrintLine(f, "Hello Kelly!")
PrintLine(f, "Hello World!")
FileClose(f)
///

If you are unsure about how to use a certain function, and you are using
VB.NET Standard or VS.NET, simply place the caret over the keyword and
press the F1 key to go to the documentation + samples.
 
C

Cor Ligthert

Kelly,
Ok, I need a little help. :) So far, I've written a program that will allow
a user to browse for a file (via a dialog box) and open the selected file.
That is extremely easy and you did that already
I need to make some slight modifications to that and have the user's data
(entered into a textbox) saved to a selected file (from the dialog
box)...here's what I have done so far. I was hoping that someone could steer
me in the right direction. Thanks!

That can be probably extremely difficult when you mean that you want to set
the sentence in a textbox and than add that to the file in a I do not know
what place.

For the txt file you do not need the notepad however you should read the
file in an array or whatever.

For the doc and the xls you need in every way you want to do it interop. You
can start for that looking here.

Office
http://support.microsoft.com/default.aspx?scid=kb;EN-US;311452

http://msdn.microsoft.com/office/

Pia Download
http://www.microsoft.com/downloads/...1e-3060-4f71-a6b4-01feba508e52&displaylang=en

I hope this gives some ideas?

Cor
 
K

Kelly

Thank you. This is extremely helpful!

-Kelly

Herfried K. Wagner said:
Kelly,



\\\
Dim f As Integer = FreeFile()
FileOpen(f, "C:\foo.txt", OpenMode.Append)
PrintLine(f, "Hello Kelly!")
PrintLine(f, "Hello World!")
FileClose(f)
///

If you are unsure about how to use a certain function, and you are using
VB.NET Standard or VS.NET, simply place the caret over the keyword and
press the F1 key to go to the documentation + samples.
 
K

Kelly

Thanks!! What I'm trying to set up is a csv file of sorts. There will be
numerous textboxes that the users will need to fill in. Then, they will
choose the file they want to save the data to (different file for each
customer) and the data will be appended to the end of the file. Later, this
will be imported into a database.

This really isn't a project that nees to be done for work. I'm really just
trying to jump into .NET and get a feel for the language and learn a few
things that will come in handy down the road for work. The program I'm
writing now will incorporate several differnet functions that I will be able
to reuse later on.

Thanks for the links!

-Kelly
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top