在文本文档中写入数值

Writing numeric value in a text document

我是 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.

您的问题在这里:

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

monhour.Value 似乎是 double 的变量类型,而您正试图将其与字符串连接。使用 ToString 函数应该有助于解决这个错误。

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

ToString MSDN