2015-09-25 113 views
0

我有一个小程序,我想保存文件,以便我可以在以后打开它时阅读它们。 我该如何保存文件,因为我必须保存5个变量并将它们读回到工具中,并且如果可能的话,我还想在Word或OpenOffice中使用文件。vb.net如何将文件保存为Word和Open Office文档?

我的变量

Title - Pieces- SinglePrice- Totalprice 

请给我以正确的方式点示例。

谢谢大家!

+0

的可能重复[如何使用C#保存doc文件](http://stackoverflow.com/questions/7811481/how-to-save-doc-file-using-c-sharp) – duDE

回答

1

如果您只想将程序中的四个变量存储在Word和OpenOffice中可以读取的文件中,则可以使用文本文件轻松完成该操作。以下代码假定Title是一个字符串,Pieces是一个整数,SinglePriceTotalPrice是十进制的。

Dim folder As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments 
Dim saveFile As String = System.IO.Path.Combine(folder, "Save.txt") 
Dim vars() As String = {Title, Pieces.ToString, SinglePrice.ToString, TotalPrice.ToString} 
System.IO.File.WriteAllLines(saveFile, vars) 

如果您需要读取文件以恢复变量的值,可以这样做。请注意,此代码假定该文件是由代码的第一个代码片段写入的,否则在使用该文件之前有必要验证文件的联系人。

Dim folder As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments 
Dim saveFile As String = System.IO.Path.Combine(folder, "Save.txt") 
Dim vars() As String = System.IO.File.ReadAllLines(saveFile) 
Title = vars(0) 
Pieces = CInt(vars(1)) 
SinglePrice = CDec(vars(2)) 
TotalPrice = CDec(vars(3))