Board logo

標題: [發問] 關於 批次創檔案 及 批次讀取 發生詭異的事情 [打印本頁]

作者: mark15jill    時間: 2013-3-11 16:47     標題: 關於 批次創檔案 及 批次讀取 發生詭異的事情

本帖最後由 mark15jill 於 2013-3-11 16:51 編輯

話說今天突然要用到批次創檔案,並在裡面加註字眼  然後選擇檔案讀取
於是呢 小小測試了一下
赫然發現 一件詭異的事情
照理來說  怎創檔案的,就應該怎讀取檔案

例如 今天有10檔案 路徑為  C:\tp\1.txt ~ C:\tp\10.txt
結果
創是創成功了
如下圖
[attach]14332[/attach]

但是呢 判斷是判斷出來,卻變分段了....
但是 程式碼內並沒有將路徑分段的符號.....

[attach]14333[/attach]

哪位大師能幫解答= =??
原始路徑
            w = File.CreateText("C:\tp\" & aks & ".txt")

程式碼內 Chr(10) 只有兩個地方
1.Button的Text
2.        '        MsgBox("C:\tp\" & "此數值為:" & Me.lis1.Text & ".txt" & Chr(10) & "存在")


以下為 原始碼

'以下程式碼為 VB2010編寫,不確定VB6.0 是否適用...
'如果要用的話,直接將以下程式碼完整複製
'不用另外創立元件,已在程式碼內創立
Imports System.IO
Public Class Form1
    Dim aks As String
    Dim bu1 As New Button
    Dim com1 As New ComboBox
    Dim lis1, lis2 As New ListBox
    Dim lab1 As New Label
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With Me
            .Controls.Add(bu1)
            .Controls.Add(com1)
            .Controls.Add(lis1)
            .Controls.Add(lis2)
            .Controls.Add(lab1)
        End With
        With bu1
            .Width = 200
            .Height = 50
            .Location = New Point(Me.Width - 250, 10)
            .Text = "創立+寫入" & Chr(10) & "讀取+List列出"
        End With
        With lab1
            .AutoSize = False
            .Location = New Point(bu1.Location.X, (bu1.Location.Y + bu1.Height) + 10)
            .Width = bu1.Width
            .Height = bu1.Height
            .BackColor = Color.Salmon
            .BorderStyle = BorderStyle.Fixed3D
        End With
        With com1
            .Width = 150
            .Height = 50
            .Location = New Point(10, 10)
            .Visible = False
        End With
        With lis1
            .Location = New Point((com1.Width + com1.Location.X) + 10, 10)
            .Height = Me.Height - 100
            .Width = 200
        End With
        With lis2
            .Location = New Point((lis1.Width + lis1.Location.X) + 10, 10)
            .Height = Me.Height - 100
            .Width = 250
        End With
        AddHandler bu1.Click, AddressOf Button1_Click
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        '若讀寫功能要分開 讀區域要另外+
        'dim kk1 as string

        '寫區域
        Dim w As StreamWriter
        Dim kk1 As String
        For akk = 1 To 500
            aks = "此數值為:" & akk
            com1.Items.Add(aks)
            kk1 = aks
            w = File.CreateText("C:\tp\" & aks & ".txt")
            w.WriteLine(kk1)
            w.Close()
        Next

        '讀區域
        Dim r As StreamReader
        For akk = 1 To 500
            aks = "此數值為:" & akk
            kk1 = aks
            r = New StreamReader("C:\tp\" & aks & ".txt")
            Do While Not r.EndOfStream
                lis1.Items.Add(r.ReadToEnd)
            Loop
            r.Close()
        Next
        AddHandler lis1.SelectedIndexChanged, AddressOf ListBox1_SelectedIndexChanged

    End Sub
    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        '讀區域
        Dim kee As String
        Dim r As StreamReader

        MsgBox(lis1.Text)
        Dim FileExists As Boolean
        'Dim filePath = "C:\tp\" & "此數值為:1" & ".txt"
        Dim filePath = "C:\tp\" & lis1.Text & ".txt"

        FileExists = My.Computer.FileSystem.FileExists(filePath)


        If FileExists = False Then


            lab1.Text = ("C:\tp\" & lis1.Text & ".txt" & "檔案 不存在")
        Else
            lab1.Text = ("C:\tp\" & lis1.Text & ".txt" & "檔案 存在")
        End If
        '        If Dir("C:\tp\此數值為:" & Me.lis1.Text & ".txt", FileAttribute.Directory) <> "" Then
        '        MsgBox("C:\tp\" & "此數值為:" & Me.lis1.Text & ".txt" & Chr(10) & "存在")
        '        End If
    End Sub
End Class
作者: mark15jill    時間: 2013-3-12 14:53

回復 1# mark15jill


    找到問題點.. 但也發現問題點= ="




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)