| View previous topic :: View next topic |
| Author |
Message |
I'm C.H. Grandmaster Cheater Supreme
Reputation: 0
Joined: 02 Dec 2006 Posts: 1000 Location: Sweden
|
Posted: Fri May 29, 2009 11:43 am Post subject: [VB.NET] Saving words into a text document. |
|
|
Yeah. I want to know how to make VB.net save the words you type from a textbox into a .txt document (and create a .txt file if it doesn't exist) in the same place as the program.
_________________
|
|
| Back to top |
|
 |
Scathe I post too much
Reputation: 0
Joined: 02 Aug 2006 Posts: 3631 Location: Smoking a blunt
|
|
| Back to top |
|
 |
shhac Expert Cheater
Reputation: 0
Joined: 30 Oct 2007 Posts: 108
|
Posted: Fri May 29, 2009 12:48 pm Post subject: |
|
|
Use it like
| Code: | Dim xyz As New ReadWriteFile
xyz.WriteTxtFile("C:\textfile.txt","Hello World!") |
| Code: | 'By Shhac
Public Class ReadWriteFile
Public FileContents As String = vbNullString, _
ErrorMessage As String = vbNullString
Private relPath As String = Application.StartupPath & "\"
Public Function clear() As Boolean
FileContents = vbNullString
ErrorMessage = vbNullString
Return True
End Function
Public Function CheckFileExists(ByVal Path As String) As Boolean
Return System.IO.File.Exists(Path)
End Function
Public Function CheckRelFileExists(ByVal RelativePath As String) As Boolean
Return CheckFileExists(relPath & RelativePath)
End Function
Public Function CheckDirectoryExists(ByVal Path As String) As Boolean
Return System.IO.Directory.Exists(Path)
End Function
Public Function CheckRelDirectoryExists(ByVal RelativePath As String) As Boolean
Return CheckDirectoryExists(relPath & RelativePath)
End Function
Public Function CreateFile(ByVal Path As String, Optional ByVal Overwrite As Boolean = False) As Boolean
If Overwrite = False Then
If CheckFileExists(Path) Then
Return False
Else
Try
Dim File As System.IO.FileStream
File = System.IO.File.Create(Path)
File.Close()
Return CheckFileExists(Path)
Catch Ex As Exception
ErrorMessage = Ex.Message
Return False
End Try
End If
Else
Try
Dim File As System.IO.FileStream
File = System.IO.File.Create(Path)
File.Close()
Return CheckFileExists(Path)
Catch Ex As Exception
ErrorMessage = Ex.Message
Return False
End Try
End If
End Function
Public Function CreateRelFile(ByVal RelativePath As String) As Boolean
Return CreateFile(relPath & RelativePath)
End Function
Public Function CreateDirectory(ByVal Path As String) As Boolean
Try
System.IO.Directory.CreateDirectory(Path)
Return CheckDirectoryExists(Path)
Catch Ex As Exception
ErrorMessage = Ex.Message
Return False
End Try
End Function
Public Function CreateRelDirectory(ByVal RelativePath As String) As Boolean
Return CreateDirectory(relPath & RelativePath)
End Function
Public Function ReadTxtFile(ByVal Path As String, Optional ByVal Encoding As String = "Unicode", Optional ByVal detectEncoding As Boolean = True) As Boolean
If Not CheckFileExists(Path) Then
Return False
Else
Dim objReader As System.IO.StreamReader
Try
objReader = New System.IO.StreamReader(Path, System.Text.Encoding.GetEncoding(Encoding), detectEncoding)
FileContents = objReader.ReadToEnd()
objReader.Close()
objReader = Nothing
Return True
Catch Ex As Exception
ErrorMessage = Ex.Message
Return False
End Try
End If
End Function
Public Function WriteTxtFile(ByVal Path As String, ByVal strData As String, Optional ByVal Append As Boolean = True, Optional ByVal Encoding As String = "Unicode", Optional ByVal ForceWrite As Boolean = False) As Boolean
If Not CheckFileExists(Path) Then
If ForceWrite Then
If CreateFile(Path) Then
Return WriteTxtFile(Path, strData, Append, Encoding, ForceWrite)
Else
Return False
End If
Else
Return False
End If
Else
Dim objWriter As System.IO.StreamWriter
Try
objWriter = New System.IO.StreamWriter(Path, Append, System.Text.Encoding.GetEncoding(Encoding))
objWriter.Write(strData)
objWriter.Close()
objWriter = Nothing
Return True
Catch Ex As Exception
ErrorMessage = Ex.Message
Return False
End Try
End If
End Function
Public Function ReadRelTxtFile(ByVal RelativePath As String, Optional ByVal Encoding As String = "Unicode", Optional ByVal detectEncoding As Boolean = True) As Boolean
Return ReadTxtFile(relPath & RelativePath, Encoding, detectEncoding)
End Function
Public Function WriteRelTxtFile(ByVal RelativePath As String, ByVal strData As String, Optional ByVal Append As Boolean = True, Optional ByVal Encoding As String = "Unicode", Optional ByVal ForceWrite As Boolean = False) As Boolean
Return WriteTxtFile(relPath & RelativePath, strData, Append, Encoding, ForceWrite)
End Function
Public Function DeleteFile(ByVal Path As String) As Boolean
Try
System.IO.File.Delete(Path)
Return Not CheckFileExists(Path)
Catch Ex As Exception
Return False
End Try
End Function
Public Function DeleteRelFile(ByVal RelativePath As String) As Boolean
Return DeleteFile(relPath & RelativePath)
End Function
Public Function DeleteDirectory(ByVal Path As String, Optional ByVal Recursive As Boolean = True) As Boolean
Try
System.IO.Directory.Delete(Path, Recursive)
Return Not CheckDirectoryExists(Path)
Catch Ex As Exception
Return False
End Try
End Function
Public Function DeleteRelDirectory(ByVal RelativePath As String) As Boolean
Return DeleteDirectory(relPath & RelativePath)
End Function
End Class
|
|
|
| Back to top |
|
 |
yoyonerd Grandmaster Cheater
Reputation: 0
Joined: 26 Apr 2008 Posts: 699 Location: -->formerly yoyonerd<--
|
Posted: Sat May 30, 2009 2:52 pm Post subject: |
|
|
Use a richtextbox, it has a built in method to save the text in itself.
I think its SaveFile().
_________________
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
|
| Back to top |
|
 |
NINTENDO Grandmaster Cheater Supreme
Reputation: 0
Joined: 02 Nov 2007 Posts: 1371
|
Posted: Sat May 30, 2009 6:08 pm Post subject: |
|
|
| slovach wrote: | http://msdn.microsoft.com/en-us/library/system.io.streamreader.readtoend.aspx
their example writes a text file, then reads it. |
I also like streamreaders/writers. Why make 1000 pages long classes for it.. It's like reinventing the wheel.
_________________
Intel over amd yes. |
|
| Back to top |
|
 |
iTz SWAT I post too much
Reputation: 1
Joined: 20 Dec 2007 Posts: 2227 Location: Me.Location;
|
Posted: Sat May 30, 2009 7:18 pm Post subject: |
|
|
It also depends, do you want the program to save the file to a set location without user control of destination, then you use:
| Code: | Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim TargetFile As StreamWriter
Try
TargetFile = New StreamWriter("C:\test.txt", True)
Catch
MessageBox.Show("Error opening " & "test.txt")
End Try
Try
TargetFile.Write(TextBox1.Text)
Catch
MessageBox.Show("Error writing file")
End Try
TargetFile.Close()
MessageBox.Show("Text saved to " & "test.txt")
End Sub |
If you would like a Save Dialog Box then:
| Code: | Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim saveFileDialog1 As System.Windows.Forms.SaveFileDialog
saveFileDialog1 = New System.Windows.Forms.SaveFileDialog()
SaveFileDialog1.CreatePrompt = True
saveFileDialog1.FileName = "text1"
SaveFileDialog1.Filter = "Word (*.doc) |*.doc;*.rtf|(*.txt) |*.txt|(*.*) |*.*"
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
Console.WriteLine(SaveFileDialog1.FileName)
End If
End Sub |
Personally I'd prefer to have the option to save where I want in a nice SaveFileDialog...
I'd choose the 2nd one...
_________________
|
|
| Back to top |
|
 |
|