Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 18-02-2009
Member
 
Join Date: Aug 2008
Posts: 38
VB.NET create Notepad File?

Hi,

I want a little help to create a text file, save & open this file from VB.Net application!

I am trying to use a textbox but dont understand how can I save & open the .txt file?
Reply With Quote
  #2  
Old 18-02-2009
Member
 
Join Date: Jan 2008
Posts: 56
Re: VB.NET create Notepad File?

I think you need to learn from this tutorial this will help you to do this!

http://www.homeandlearn.co.uk/NET/nets8p2.html

I hope this helps you!
Reply With Quote
  #3  
Old 18-02-2009
Member
 
Join Date: May 2008
Posts: 63
Re: VB.NET create Notepad File?

You can learn it with the help of this pdf file too1

http://www.elearning.strathmore.edu/..._Textfiles.pdf
Reply With Quote
  #4  
Old 18-02-2009
Member
 
Join Date: May 2008
Posts: 27
Re: VB.NET create Notepad File?

Ok this is the code you needed!

Code:
Imports System
Imports System.IO
Imports System.Windows.Forms
 
Namespace MoreFileOps2
    Public partial Class Form1
	 Inherits Form
        Private filename As String =  "" 
 
        Public  Sub New()
            InitializeComponent()
        End Sub
 
        Private  Sub OpenButton_Click(ByVal sender As Object, ByVal e As EventArgs)
            OpenFile()
        End Sub
 
        Private  Sub OpenFile()
            Dim openFileDialog As OpenFileDialog =  New OpenFileDialog() 
            openFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
            openFileDialog.CheckFileExists = False 'it would check for us, but we want to do this ourselves
            openFileDialog.DefaultExt = "txt"      'they dont have to enter an extension if they dont want to
            openFileDialog.AddExtension = True
            If openFileDialog.ShowDialog(Me) = DialogResult.OK Then
                Try
                    'if the file doesn't exist we'll throw our own exception
                    'because ReadAllText will try to read a nonexistant file
                    'and set our textbox to empty
                    If Not File.Exists(openFileDialog.FileName) Then
                        Throw New FileNotFoundException()
                    End If
                    richTextBox1.Text = File.ReadAllText(openFileDialog.FileName)     'this fills the textbox and closes the file
                    filename = openFileDialog.FileName                 'save the filename for later. this also serves as a flag that a file is considered open  
 
                Catch FileNotFoundException
                    'This file does not exist. We'll ask them if they wish to create one
                    CreateSpecifiedFile(openFileDialog.FileName)
 
                Catch ex As Exception
                    MessageBox.Show(ex.Message)
                End Try
 
            End If
 
            'Notice that we are not actually leaving the file open. Since we are not writing to the file
            'unless they click a save button, it doesnt need to stay open physically. The advantage is when they exit
            'the program we don't have any file cleanup to do. The user doesn't know the file
            'doesnt STAY open, and they dont care. As long as there is a filename in the filename var
            'the file is virtually open. This is mostly just programmers preference, I like it. Its clean, simple and
            'requires less file maintenance
        End Sub
 
        Private  Sub richTextBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
            'this is a way to handle the error case if there is nothing in the textbox
            'when save is clicked. We prevent the error from occurring by not enabling the buttons unless
            'there is something to save. Another way to handle this would be always have the buttons enabled and put up a messagebox saying
            'the textbox is empty, inside the save button handlers.
            If richTextBox1.Text <> String.Empty Then
                SaveButton.Enabled = True
                SaveAsButton.Enabled = True
            Else 
                SaveButton.Enabled = False
                SaveAsButton.Enabled = False
            End If
        End Sub
 
        Private  Sub SaveButton_Click(ByVal sender As Object, ByVal e As EventArgs)
            SaveFile()
 
        End Sub
 
        Private  Sub SaveFile()
            'if we have a filename already, just save it
            If filename <> "" Then
                'this handy utility opens the file, writes the text and closes the file
                File.AppendAllText(filename, richTextBox1.Text)
 
            Else 
                'we don't have a filename, so ask them if they want to create one
                CreateFile()
 
            End If
        End Sub
 
        Private  Sub CreateFile()
            If MessageBox.Show("No file has been specified. Do you wish to create one?","Create new file?",MessageBoxButtons.YesNoCancel) = DialogResult.Yes Then
                'they do so call the SaveAsFile routine
                SaveAsFile()
            End If
        End Sub
 
 
        Private  Sub SaveAsButton_Click(ByVal sender As Object, ByVal e As EventArgs)
            SaveAsFile()
        End Sub
 
        Private  Sub SaveAsFile()
            'They want to do a SaveAs, so find out what they want to name the file
            Dim saveFileDialog As SaveFileDialog =  New SaveFileDialog() 
            saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
            saveFileDialog.DefaultExt = "txt"
            saveFileDialog.AddExtension = True
            If saveFileDialog.ShowDialog(Me) = DialogResult.OK Then
                filename = saveFileDialog.FileName                 'save the filename for later. this also serves as a flag that a file is open  
                'our handy utility creates the file if needed
                File.AppendAllText(filename, richTextBox1.Text)
            End If
 
        End Sub
 
        Private  Sub CreateSpecifiedFile(ByVal filename As String)
            If MessageBox.Show(String.Format("File {0} does not exist. Do you wish to create it?",filename),"Create new file?",MessageBoxButtons.YesNoCancel) = DialogResult.Yes Then
                Me.filename = filename       'save just the filename cause there is nothing to write
            End If
        End Sub
 
    End Class
End Namespace
 
'----------------------------------------------------------------
' Converted from C# to VB .NET using CSharpToVBConverter(1.2).
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "VB.NET create Notepad File?"
Thread Thread Starter Forum Replies Last Post
Unable to save file in Notepad++ Cade Windows Software 5 17-02-2010 03:33 AM
How to Use Notepad to Create a Log File neonxgenesis Windows Software 3 17-11-2009 08:25 AM
How to use VBA to Open an excel file in notepad cornto Software Development 3 09-10-2009 10:36 AM
How can I create notepad in C++ AbhayD Software Development 3 02-09-2009 04:59 PM


All times are GMT +5.5. The time now is 01:20 PM.