不在vb.net中保存.txt文件

时间:2012-08-26 20:21:27

标签: vb.net text streamwriter

切入追逐:

Function Create(ByVal network, ByVal location, ByVal type, ByVal requirement1, ByVal requirement2, ByVal requirement3, ByVal name)
    Dim net As String = network
    Dim loc As String = location
    Dim typ As String = type
    Dim nam As String = name
    Dim req1 As String = requirement1
    Dim req2 As String = requirement2
    Dim req3 As String = requirement3
    Dim Mission As New System.IO.StreamWriter("C:\" & nam & ".txt")
    Mission.WriteLine("Name: " & net)
    Mission.WriteLine("Network: " & net)
    Mission.WriteLine("Location: " & loc)
    Mission.WriteLine("Type: " & typ)
    Mission.WriteLine("Requirement: " & req1)
    Mission.WriteLine("Requirement: " & req2)
    Mission.WriteLine("Requirement: " & req3)
    Mission.Close()
    Console.WriteLine("Written")
    System.Threading.Thread.Sleep(3000)
End Function

不会出现错误,但文件路径中的文件也不会出现。当我调用函数时,我已经将所有声明都声明了,所以我知道它不是问题所在。 救命? :)

1 个答案:

答案 0 :(得分:0)

Sub Create(ByVal network As String, ByVal location As String, 
    ByVal type As String, ByVal requirement1 As String, 
    ByVal requirement2 As String, ByVal requirement3 As String,
    ByVal name As String)

    Dim net As String = network
    Dim loc As String = location
    Dim typ As String = type
    Dim nam As String = name
    Dim req1 As String = requirement1
    Dim req2 As String = requirement2
    Dim req3 As String = requirement3
    If Not nam.EndsWith(".txt") Then
        nam &= ".txt"
    End If
    Dim Mission As New System.IO.StreamWriter(Path.Combine(
      System.Environment.GetFolderPath(
      System.Environment.SpecialFolder.Desktop), nam))
    Mission.WriteLine("Name: " & net)
    Mission.WriteLine("Network: " & net)
    Mission.WriteLine("Location: " & loc)
    Mission.WriteLine("Type: " & typ)
    Mission.WriteLine("Requirement: " & req1)
    Mission.WriteLine("Requirement: " & req2)
    Mission.WriteLine("Requirement: " & req3)
    Mission.Close()
    Console.WriteLine("Written")
    'System.Threading.Thread.Sleep(3000)
End Sub

首先,你将参数作为对象传递!!,第二,你没有权限保存到c根目录,除非你有管理员权限,第三,最后的线程部分是完全没用的,它阻止了UI并且不会累积任何好处。
最后但并非不重要。 祝你好运