Code: Select all
Private Function LoadFile( _
ByVal Filter1 As String, _
Optional ByVal DialogTitle As String = "Open", _
Optional ByVal MultiSelect As Boolean = False, _
Optional ByVal ErrorMsg As String = "An error has occured." _
) As String
Dim Open As New OpenFileDialog()
Dim myStreamReader As System.IO.StreamReader
Dim text As String = ""
Dim Filter2 As String = Filter1
Dim DialogTitle2 As String = DialogTitle
Dim EMsg As String = ErrorMsg
Dim Multi As Boolean = MultiSelect
Open.Filter = Filter2
Open.CheckFileExists = True
Open.Title = DialogTitle2
Open.Multiselect = Multi
Open.ShowDialog(Me)
Try
Open.OpenFile()
myStreamReader = System.IO.File.OpenText(Open.FileName)
text = myStreamReader.ReadToEnd()
Catch ex As Exception
MsgBox(EMsg)
End Try
Return text
End Function
Code: Select all
data = LoadFile("FileType (*.txt)|*.txt", "Open Text File", False)
Code: Select all
Private Function SaveFile( _
ByVal Data As String, _
ByVal Filter1 As String, _
Optional ByVal DialogTitle As String = "Save", _
Optional ByVal ErrorMsg As String = "An error has occured." _
) As String
Dim Save As New SaveFileDialog()
Dim myStreamWriter As System.IO.StreamWriter
Dim Data2 As String = Data
Dim Filter2 As String = Filter1
Dim DialogTitle2 As String = DialogTitle
Dim EMsg As String = ErrorMsg
Save.Filter = Filter2
Save.CheckPathExists = True
Save.Title = DialogTitle2
Save.ShowDialog(Me)
Try
myStreamWriter = System.IO.File.CreateText(Save.FileName)
myStreamWriter.Write(Data2)
myStreamWriter.Flush()
Catch ex As Exception
MsgBox(EMsg)
End Try
Return Save.FileName
End Function
Code: Select all
SaveFile(data, "FileType (*.txt)|*.txt", "Save Text File")
Code: Select all
filename = SaveFile(data, "FileType (*.txt)|*.txt", "Save Text File")
I know, there are a few problems like no init directory and maybe more, but i am working on them. i would fix it but i am very busy on my latest project, HMMN, which uses these function plus other functions i wrote. check it out when its done!
ok like usual, if you need more help, if theres any problems with the code, just PM me, AIM me, Email me, or just leave a message in this forum. thanks everyone.
*mods please sticky, lots of people seem to have problems opening/saving files, thanks*