解决方案:
对于有一个.的文件名如:a.sln
参照以下A方法
对于有二个.以上的文件名如:a.b.sln
通过FileStream类和StreamWriter来 删除原始文件的内容再写入新文件的内容。
参照以下B方法
Public Shared Sub Write_File_A(ByVal str As StringBuilder)
Dim fFile1 As FileInfo = Nothing
fFile1 = New FileInfo(mstrPath)
Dim sw As StreamWriter
fFile1.Attributes = CType(FileAttribute.Normal,FileAttributes)
’利用CreateText()来写入新内容。AppendText()是在尾部追加内容。
sw = fFile1.CreateText()
'sw = fFile1.AppendText()
sw.Write(str)
sw.Flush()
sw.Close()
fFile1.Attributes = CType(FileAttribute.ReadOnly,FileAttributes)
fFile1.Refresh()
fFile1 = Nothing
End Sub
Public Shared Sub Write_File_B(ByVal str As StringBuilder)
Dim fFile1 As FileInfo = Nothing
fFile1 = New FileInfo(mstrPath)
'修改文件属性:只读文件-》存档文件
fFile1.Attributes = CType(FileAttribute.Normal,FileAttributes)
fFile1.Refresh()
fFile1 = Nothing
Dim Fs As FileStream = New FileStream(mstrPath,_
FileMode.Truncate,FileAccess.ReadWrite,FileShare.None)
Dim SwFromFileStream As StreamWriter = New StreamWriter(Fs,System.Text.Encoding.GetEncoding(strEncoding))
SwFromFileStream.Write(str)
SwFromFileStream.Flush()
SwFromFileStream.Close()
Fs.Close()
fFile1 = New FileInfo(mstrPath) fFile1.Attributes = CType(FileAttribute.ReadOnly,FileAttributes) fFile1.Refresh() fFile1 = Nothing End Sub
原文链接:https://www.f2er.com/vb/260945.html