有人可以帮助在VB.Net中重写此功能 [英] Can someone help to rewrite this function in VB.Net

查看:109
本文介绍了有人可以帮助在VB.Net中重写此功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码来自VB Scripting函数,同样的代码在VB.Net中不起作用。可以帮助我在VB.Net中重写这个函数。任何有关在函数中实现错误处理的帮助都非常感谢。此函数基本上将与.txt文件相同的.INI文件读入数组。如果文件不存在,请确保首先我们需要验证文件是否存在然后我们需要退出该函数。



This below code is from VB Scripting function and the same code does not work in VB.Net.Can some one help me to rewrite this function in VB.Net. Any help with implementing error handling in the function is highly appreciated. This functions basically reads a .INI file same as .txt file into an array. Please make sure that first we need to validate if the file existence before proceeding if the file is not there we need to exit the function.

Public Shared Function ReadFileIntoArray(sFileName As String, asFileContents() As String, _
                            vRemoveBlankLines As Object) As Integer
    Dim Result As Integer
    Dim Result1 As Integer
    Dim Result2  As Integer    
    Dim Position As Integer
    Dim iFileNum As Integer
    Dim sLineInput As String
    Dim iRowCount As Integer
    Dim sErrMessage As String
    Dim sFunctionName As String
    Dim strFileName As String
    sFunctionName = "ReadFileIntoArray"
    On Error GoTo ErrorHandler
    
    ReadFileIntoArray = -1
    'Validate file existence
     sErrMessage = "File not found = " & sFileName
     If sFileName <> False Then
        Console.WriteLine("& sErrMessage, (0+48+0)")
        ReadFileIntoArray = 0
        Exit Function
    Else
       Console.WriteLine("File found & sFileName")
    
    End If

    'validate optional parameter
    'If IsMissing(vRemoveBlankLines) Then
     '   vRemoveBlankLines = False       'default condition
   ' End If
    
    iFileNum = Freefile
    
    Open strFileName For Input As #iFileNum 
    
    Do Until EOF(iFileNum)
        Line Input #iFileNum, sLineInput
        If vRemoveBlankLines = True Then
            If Trim(sLineInput) <> "" Then
                Position = Instr(sLineInput, ";")
                If Position = 0 Then
                   iRowCount = iRowCount + 1
                   ReDim Preserve asFileContents(iRowCount)
                   asFileContents(iRowCount) = sLineInput
                End If   
            End If
        Else
            iRowCount = iRowCount + 1
            ReDim Preserve asFileContents(iRowCount)
            asFileContents(iRowCount) = sLineInput
        End If
    Loop

    Close #iFileNum 

    ReadFileIntoArray = iRowCount
    Exit Function
    
ErrorHandler:
'    MsgBox "Error number " & Err & " occurred at line: " & Erl
End Function

推荐答案

你真正需要的只是ReadAllLines函数,你应该自己做其余的事情:

http://msdn.microsoft.com/en-us/library/s2tte0y1.aspx [ ^ ]
All you really need is the ReadAllLines function, and you shoul dbe able to do the rest yourself:
http://msdn.microsoft.com/en-us/library/s2tte0y1.aspx[^]


这篇关于有人可以帮助在VB.Net中重写此功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆