2012-05-18 37 views
1

有一個INI文件需要從中訪問和讀取信息。這是有問題的INI文件的完整內容:http://www.heypasteit.com/clip/0C3Q使用vb.net在文本文件中搜索特定字符串2010

幾個人建議我的代碼,但他們不工作。我相信這是因爲INI文件中的[ ]標籤。因爲他們工作,如果我刪除標籤。

我的程序有一堆組合框,軌跡條和複選框。這些項目將由從INI文件中獲取的信息填充。

例如,ini文件具有這些行;

... 
bCrosshairEnabled=1 
bDoDepthOfField=0 
bFXAAEnabled=1 
uiMaxSkinnedTreesToRender=10 
iSize H=720 
iSize W=1280 
... 

例如:我想在我的形式checkbox8獲取或檢查是否bFXAAEnabled1值未選中,如果它是0

請確保您的代碼兼容VB.NET 2010

回答

1

你可以試試這個

Dim myvalue As Integer 
    For Each k As String In IO.File.ReadLines("d:\test.txt") 
     If k.Contains("valueccc=") Then 
      Dim values() As String = k.Split(CChar("=")).ToArray 
      myvalue = Convert.ToInt32(values(1)) 
     End If 
    Next 
Select case myvalue 
case 1 
case 2 
End select 
+0

這不會選項嚴格在編譯! –

+0

已編輯,它現在應該能夠嚴格支持。謝謝。 – surpavan

+0

ToArray選項給出錯誤。有「ToString」可用雖然... –

0
'remember to import this at the top of your class 
    Imports System.IO 


    Dim filename As String = "C:\file.txt" 
    Dim parts() As String = Nothing 

    'use a stream reader to read the file line by line 
    Using sr As New StreamReader(filename) 
     'read a line as split into parts at the equal sign 
     parts = sr.ReadLine.Split("="c) 
     'check we actually have read the data in the correct format 
     If parts.Length >= 2 Then 
      Select Case parts(0) 
       'use a case statement for the left hand side 
       Case "valuexyz" 
        'now use a case statement for the right hand side 
        Select Case parts(1).Trim 
         Case "0" 
          Foo() 
         Case "1" 
          Bar() 
        End Select 

       Case "valueabc" 
        Select Case parts(1).Trim 
         Case "0" 
          Foo2() 
         Case "1" 
          Bar2() 
        End Select 
      End Select 
     End If 
    End Using 
+0

好的,我試過你的代碼,但沒有運氣.. –

+0

沒有運氣? - 你能明白出了什麼問題嗎?你是否收到錯誤信息? –

+0

不,我不會有任何錯誤。事情是,代碼不會做任何事情。讓我告訴你我的VB.net代碼屏幕:http://postimage.org/image/b5xsq83e3/5fde8101/ 我正在製作的這個程序需要閱讀Skyrim的一個視頻遊戲選項(它將其設置存儲在.ini文件),然後程序必須在複選框上反映這些設置。如果該ini文件中的值設置爲「1」,那麼我的程序需要讀取該值並將複選框的選中狀態設置爲「true」。 –

1
'Reads each line from the text file one at a time 
    For Each line As String In IO.File.ReadLines("text file path") 

     'split the string by equals sign 
     Dim ary As String() = line.Split("="c) 

     'Check the data type of the string we collected is inline with what we are expecting, e.g. numeric 
     If IsNumeric(ary(1)) Then 

      'create key value pair: the string before the equals and the number after it 
      'e.g. 
      'key = "valuexyz" | value = "36"" 
      Dim valuePair As New KeyValuePair(Of String, Integer)(ary(0), CInt(ary(1))) 

      'obtain the string after the equals sign 
      Dim value As Integer = CInt(ary(1)) 

      'based on the value after the equals sign, do something 
      Select Case value 
       Case 1 
        ' do something using.. 
        'valuePair.Key - this is the string before the equals 
        'valuePair.Value - this is the string after the equals 
       Case 2 
        ' do something using.. 
        'valuePair.Key - this is the string before the equals 
        'valuePair.Value - this is the string after the equals 
       Case Else 
        ' do something using.. 
        'valuePair.Key - this is the string before the equals 
        'valuePair.Value - this is the string after the equals 
      End Select 

     End If 

    Next 
+0

但是,我如何知道數字是從valuexyz還是valueabc? –

+0

@Çağan這不是你問的。提高你的問題的質量,你會得到更多有用的答案... – Mikaveli

+0

你的代碼從純文本文件讀取時工作得很好。但從這裏閱讀時它不起作用:http://www.heypasteit.com/clip/0C3Q。我相信原因是文件中的「[]」標籤。我該如何克服這一點? –

0

在你的底殼聲明你Checkbox8.checked屬性設置爲在任何一方面都是虛假的。決定在哪種情況下,您需要將複選框設置爲選中狀態,然後寫入相應的行來執行此操作。

+0

這應該是對另一個答案的評論? –

+0

是的,這是爲了迴應評論 - 「在編寫代碼的情況下,複選框始終保持未選中,無論.ini文件中的值是否設置爲」1「或」0「。」我看不到如何直接回應評論。有沒有辦法做到這一點? –

0

從您的意見我的其他答案我張貼有關如何從一個INI文件中讀取值的新的答案推斷:

Imports System.Text 
Imports System.Runtime.InteropServices 

Public Class TestForm 
    'declare the API 
    <DllImport("kernel32.dll", SetLastError:=True)> _ 
    Private Shared Function GetPrivateProfileString(ByVal lpAppName As String, _ 
         ByVal lpKeyName As String, _ 
         ByVal lpDefault As String, _ 
         ByVal lpReturnedString As StringBuilder, _ 
         ByVal nSize As Integer, _ 
         ByVal lpFileName As String) As Integer 
    End Function 

    'Function to retrieve a value from an INI file 
    Public Function GetINIValue(filename As String, section As String, key As String, Optional defaultValue As String = "") As String 
     Dim res As Integer 
     Dim sb As New StringBuilder(500) 
     res = GetPrivateProfileString(section, key, "", sb, sb.Capacity, filename) 
     If res = 1 Then Return sb.ToString Else Return defaultValue 
    End Function 

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 
     Dim filename As String = "C:\Scratch\Test.ini" 
     CheckBox1.Checked = If(GetINIValue(filename, "Display", "bFXAAEnabled") = "1", True, False) 
    End Sub 

End Class 
+0

非常感謝你和其他人爲你解決我煩人的問題所做的努力。但是,有一件事,當我粘貼以「DLLImport」開頭的代碼時,我得到如下錯誤:「Type'DllImport'未定義。」,「Type'StringBuilder'未定義。」,「Function'GetPrivateProfileString'doesn' t返回所有代碼路徑的值,你是否缺少'Return'語句? –

+0

http://s13.postimage.org/ngsn3c5cn/desto.png –

+0

是的,我的壞,修復它。這個吸盤:「'GetINIValue'沒有聲明。由於它的保護級別,它可能無法訪問。「在粘貼第三個代碼之後發生,使用複選框。lol –

相關問題