在文本文档中编写数值

时间:2015-05-28 14:25:41

标签: vb.net type-conversion

我是VB.net的新手,我成功地编写了一个文本文件。我正在尝试在文本文档中存储许多数值的条目。我写的代码是:

Imports System
Imports System.IO
Imports System.Text
Public Class set_time

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If (Not System.IO.Directory.Exists("C:\MainRegistry")) Then
        System.IO.Directory.CreateDirectory("C:\MainRegistry")
    End If
    If System.IO.File.Exists("C:\MainRegistry\MainRegistry.txt") = True Then

        System.IO.File.Delete("C:\MainRegistry\MainRegistry.txt")


    End If

    Dim MainRegistry As New System.IO.StreamWriter("C:\MainRegistry\MainRegistry.txt", True)
    MainRegistry.Write(monhour.Value + "," + monmin.Value)
    MainRegistry.Close()
End Sub
End Class

问题是我在Write()行收到错误:Conversion from string "," to type 'Double' is not valid.

1 个答案:

答案 0 :(得分:0)

您的问题在这里:

MainRegistry.Write(monhour.Value + "," + monmin.Value)

monhour.Value似乎是double的变量类型,而您尝试将其与字符串连接。使用ToString函数可以帮助解决此错误。

MainRegistry.Write(monhour.Value.ToString + "," + monmin.Value.ToString)

ToString MSDN