扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
VB.NET编程读取txt文本文档中的数据,并把数据保存为数组,代码如下:
成都创新互联公司专注于网站建设|成都网站维护公司|优化|托管以及网络推广,积累了大量的网站设计与制作经验,为许多企业提供了网站定制设计服务,案例作品覆盖会所设计等行业。能根据企业所处的行业与销售的产品,结合品牌形象的塑造,量身设计品质网站。
'写配件文件
Private Sub saveIni(ByVal filePath As String, ByVal str As String)
Dim sw As StreamWriter = New StreamWriter(filePath, True) 'true是指以追加的方式打开指定文件
sw.WriteLine(str)
sw.Flush()
sw.Close()
sw = Nothing
End Sub
'读配件文件
Private Function readIni(ByVal filePath As String)
Dim iniDt As New DataTable
iniDt.Columns.Add("text")
iniDt.Columns.Add("value")
Try
Dim sr As StreamReader = New StreamReader(filePath, System.Text.Encoding.Default)
Dim line As String = ""
While Not sr.EndOfStream
Dim str = sr.ReadLine()'读取当前行
iniDt.Rows.Add(New String() {
str(0),
str(1)
})
End While
sr.Close()
sr = Nothing
Catch ex As Exception
End Try
Return iniDt
End Function
Dim WithEvents SvaeFileDialog1 As New SaveFileDialog
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles button1.Click
Dim pp As String
SvaeFileDialog1.Filter = "文档文件(*.txt)|*.txt|所有文件(*.*)|*.*"
Dim r As MsgBoxResult
r = SvaeFileDialog1.ShowDialog
If r Then
FileOpen(1, SvaeFileDialog1.FileName, OpenMode.Output)
pp = Mid(Me.TextBox1.Text, 1, Len(Me.TextBox1.Text))
Print(1, pp)
Close()
MsgBox("Export Successfully!")
End If
End Sub
以上VS2010 WPF程序。
分类: 电脑/网络 程序设计 其他编程语言
问题描述:
我用的是Timer计时器:
如何将程序控制住,让它第一次发现没保存后,只击活一次提示保存,然后使用修改后的保存路径:(代码如下)
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'如何将程序控制住,让它第一次发现没保存后,只击活一次提示保存,然后使用修改后的保存路径
If SaveFileDialog1.FileName = "" Then
If SaveFileDialog1.ShowDialog Then
rtbox.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.PlainText)
End If
Else
'如果已经选择了要保存的文件名,则保存文本到文件中
rtbox.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.PlainText)
End If
End Sub
————————————————————
此代码执行后变成死循环。
解析:
经过我的潜心修炼问题终于是解决了.请看代码
Imports System.IO
Private strFileName As String = "myRTFdoc.txt"
Private flgFirst As Boolean = True
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
'如何将程序控制住,让它第一次发现没保存后,只击活一次提示保存,然后使用修改后的保存路径
Call zc()
End Sub
Private Sub zc()
'Timer2.Stop()
With SaveFileDialog1
.DefaultExt = "txt"
.FileName = strFileName
.Filter = "Text files(*.txt)|*.txt|All files(*.*)|*.*"
.FilterIndex = 1
.InitialDirectory = "c:\"
.OverwritePrompt = True
.Title = "Save Reminding"
End With
'Timer2.Enabled = False
If flgFirst = True Then
If SaveFileDialog1.ShowDialog = DialogResult.OK Then
strFileName = SaveFileDialog1.FileName
Dim objWriter As StreamWriter = New StreamWriter(strFileName, False)
objWriter.Write(rtbox.Text)
objWriter.Close()
objWriter = Nothing
End If
flgFirst = False
'Timer2.Enabled = True
' Timer2.Start()
Else
'flg= second
Dim objWriter As StreamWriter = New StreamWriter(strFileName, False)
objWriter.Write(rtbox.Text)
objWriter.Close()
objWriter = Nothing
End If
'Timer2.Enabled = True
'Timer2.Start()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Timer2.Enabled = True
' zc()
End Sub
知道问题在哪里嘛?我调试了确实是"死循环"、其实不是真正的死循环、是time tick事件你设定的时间太短了.估计只设置了1-5s左右.那么程序运行还需要时间.所以他就不停地调用timetick时间.你的savedialog对话框根本来不急弹出来.
所以建议你把timer的interval的值设置高点最好12-15秒 触发一次.
当然我想还有其他的方法、还没有彻底取研究下.
比如stop 什么 、那还需要时间.
至少目前我的方法是可以了.
我设置的12s P4. 3.0 1G 内存 跑下我的程序如果 F10(F8)慢点的话就来不及了.
另为把你的程序小改了下、应该可以满足你的要求了.
直接给你保存和读取TXT的VB.NET的函数代码,你只要在触发事件中调用就可以了,注意换行,有的是显示不下去才显示两行的,还有strFilePath代表文件路径,TempENG代表文件编码格式如:UTF-8或者GB2312,strText 代表内容字符串。
#Region "读取TXT"
Public Function ReadTxt(ByVal strFilePath As String, ByVal TempENG As String) As String
Dim mySr As System.IO.StreamReader
Dim strS As String
Dim n%
strS = ""
mySr = New System.IO.StreamReader(strFilePath, System.Text.Encoding.GetEncoding(TempENG))
Do
Dim line$ = mySr.ReadLine()
strS = strS line vbCrLf
n = n + 1
Loop Until mySr.EndOfStream = True
mySr.Close()
Return strS
End Function
#End Region
#Region "保存TXT"
Public Function SaveTxt(ByVal strFilePath As String, ByVal strText As String, ByVal TempENG As String) As Boolean
Try
If Dir(strFilePath) "" Then File.Delete(strFilePath)
Dim mySw As System.IO.StreamWriter
Dim strLine() As String
mySw = New System.IO.StreamWriter(strFilePath, True, System.Text.Encoding.GetEncoding(TempENG))
strLine = Split(strText, vbCrLf)
For i As Integer = 0 To UBound(strLine)
mySw.WriteLine(strLine(i))
Next
mySw.Close()
Return True
Catch ex As Exception
Return False
End Try
End Function
#End Region
VBweb保存代码如下:
Private Sub mnusave_Click() '保存
Set fs = CreateObject("Scripting.FileSystemObject")
CommonDialog1.InitDir = "c:\"
CommonDialog1.Filter = "文本文件|*.txt"
If fs.fileexists(FileName) = False Then
CommonDialog1.ShowSave
End If
FileName = CommonDialog1.FileName
RichTextBox1.SaveFile FileName, rtfText
Me.Caption = "Easy Notebook -" FileName
End Sub
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流