Save File from a Win Form application

S

Scott M. Lyon

I'm working on a WinForm application, and recently was informed that I need
to create an "Export" function for the data (stored in SQL Server), in a
specific (TXT, tab delimited) format.


I'd like to figure out a way to bring up the "Open or Save" dialog similar
to what we see on Web Pages, but with a minimal amount of coding (especially
considering this isn't a Web application), but would then allow me to create
a TXT file and either open it (in whatever they have set as the default
application for TXT files, like Notepad) or save it (in which case they'd
get the standard SaveAs dialog)


Is there an easy way to do this in .NET?


Thanks!
 
G

Guest

Start a new Windows application

Add two buttons to it (Button1 & Button2 respectively)

Double-click an 'OpenFileDialog' control (its added to the project)

Double-click an 'SaveFileDialog' control (its added to the project)

Double-click Button1 & paste in the following code:

With OpenFileDialog1
.DefaultExt = "txt"
.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
.FilterIndex = 0
.InitialDirectory =
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
If .ShowDialog = DialogResult.OK Then
strOpenFilename = .FileName
Else
Exit Sub
End If
End With
' Do stuff here

-----------------------

Double-click Button2 & paste in the following code:

With SaveFileDialog1
.DefaultExt = "txt"
.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
.FilterIndex = 0
.InitialDirectory =
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
If .ShowDialog = DialogResult.OK Then
strSaveFilename = .FileName
Else
Exit Sub
End If
End With
' Save tab delimited data here

--------------------------------------------------------

This is the code behind Button2, but with a tab delimited section at the end:

With SaveFileDialog1
.DefaultExt = "txt"
.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
.FilterIndex = 0
.InitialDirectory =
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
If .ShowDialog = DialogResult.OK Then
strSaveFilename = .FileName
Else
Exit Sub
End If
End With
' Save tab delimited data here
' Example:
Dim sw As New IO.StreamWriter(strSaveFilename)
sw.WriteLine("Some Data" & ControlChars.Tab & "Some More Data...")
sw.Flush()
sw.Close()
 

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