Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Save File Dialog create new file

Posted on 2006-06-01
11
Medium Priority
?
748 Views
Last Modified: 2008-02-01
Hi Experts,
When I click on save in my SaveFileDialog, I get a popup that says "C:\test2.txt does not exist. Please verify that the correct file name is given."

How do I create a file using the name entered in the SaveFileDialog if it doesn't exist?

This is the code I used to show the dialog and writing text to the text file:

If ExportFileDialog.ShowDialog = DialogResult.OK Then
            Dim FILE_NAME As String = ExportFileDialog.FileName
            Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
            objWriter.Write(TextBox1.Text)
            objWriter.Close()
End If

Thanks
noobe1
0
Comment
Question by:noobe1
  • 4
  • 3
8 Comments
 
LVL 12

Expert Comment

by:vb_jonas
ID: 16810034
Hmm... thats the default behavior of an OpenFileDialog. If you declared it as a SaveFileDialog - check your properties for the ExportFileDialog.
0
 
LVL 8

Expert Comment

by:gena17
ID: 16810246
Try to specify "C:\\test2.txt" as constant file name and see what you get.
0
 

Author Comment

by:noobe1
ID: 16811194
Hi gena17,

Do yo mean specifying the name "C:\test2.txt" in the code?

Thanks
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 12

Expert Comment

by:vb_jonas
ID: 16811391
       Dim ExportFileDialog As New SaveFileDialog
        If ExportFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim FILE_NAME As String = ExportFileDialog.FileName
            Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
            objWriter.Write(TextBox1.Text)
            objWriter.Close()
        End If

works fine.
0
 

Author Comment

by:noobe1
ID: 16811543
Hi vb,

The code will overwrite the file if it exists without asking the user if he/she wants to overwrite it.

Thanks


0
 

Author Comment

by:noobe1
ID: 16811681

I found the solution to my problem. :)

Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
        Dim ExportFileDialog As New SaveFileDialog
        ExportFileDialog.CheckFileExists=False
        If ExportFileDialog.ShowDialog = DialogResult.OK Then
            Dim FILE_NAME As String = ExportFileDialog.FileName
            If System.IO.File.Exists(FILE_NAME) = True Then
                If MessageBox.Show("File already exist. Do you want to overwrite the existing file", "", MessageBoxButtons.YesNo) = DialogResult.Yes Then
                    Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
                    objWriter.Write(TextBox1.Text)
                    objWriter.Close()
                Else
                    btnExport_Click(sender, e)
                    Exit Sub
                End If
            Else
                Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
                objWriter.Write(TextBox1.Text)
                objWriter.Close()
            End If
        End If
    End Sub

0
 
LVL 12

Accepted Solution

by:
vb_jonas earned 1000 total points
ID: 16813731
Hi, good, but it is really enough with my code. It does not overwrite if the file exists, since the default behavior of the SaveFileDialog is to ask whether to overwrite or not. But if you set the CheckFileExists to false it wont ask.

0
 
LVL 12

Expert Comment

by:vb_jonas
ID: 16987094
My code works, as I mentioned in my last comment. :-)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Screencast - Getting to Know the Pipeline
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question