一个经典的问题(从.net将字符窜写入文本)

[复制链接]
查看11 | 回复0 | 2006-8-25 11:04:00 | 显示全部楼层 |阅读模式
代码功能:将.net中的字符窜输入.txt文本
方法一:
在NetPage.vb(这个文件存放类)中定义一个写入文本的函数,如下:
//倒入所需文件
Imports System.Web
Imports System.Data
Imports System.IO
Imports System.Data.SqlClient
Namespace MyNetPage
Public Class NetPage
'写入文本的字符窜
Public WriteOnly Property WriteLineStr() As String

Set(ByVal Value As String)

WriteStr = Value

End Set
End Property
'文本的路径名
Public WriteOnly Property FileNamePath() As String

Set(ByVal Value As String)

FileName = Value

End Set
End Property
'将字符窜写入文本
Public Function WriteToFile()

Dim FileWriteObj As StreamWriter -----成功

FileWriteObj = File.AppendText("FileName&quot

------成功

FileWriteObj.WriteLine(WriteStr) ------没有成功

FileWriteObj.Close()
End Function
在WebForm1.aspx.vb中有如下代码:
Imports System.Data
Imports test_1.MyNetPage
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim WriteString as String = "HELLO"
Dim Net = New NetPage
Net.WriteLineStr = WriteString
Net.FileNamePath = "D:\test.txt"
Net.WriteToFile()
End Sub
出现的状况是:测试下来要输入文本的字符窜(WriteString)和文本名(D:\test.txt)都传送到了WriteToFile()这个函数中,但最后一句FileWriteObj.WriteLine(WriteStr)却没有执行,文本中没有"HELLO"被输入。
我们再来看下不用函数,直接在WebForm1.aspx.vb中写输入文本的代码:
方法二:
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports test_1.MyNetPage
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim WriteString As String = "HELLO"
Dim strfileobj As StreamWriter
strfileobj = File.AppendText("D:\test.txt&quot


strfileobj.WriteLine(WriteString)
strfileobj.Close()
End Sub
测试结果成功将HELLO写入了test.txt文本。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行