如何在VB.NET中找到头文件 [英] How do I find header file in VB.NET

查看:321
本文介绍了如何在VB.NET中找到头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我无法循环找到标题因为结果总是0

谢谢



什么我试过了:



my problem is i can't loop to find the header cause the result is always 0
thanks

What I have tried:

        Dim headerarray(0 To 3) As Byte
        Dim result, count, pos As Integer
        Dim files As String = FreeFile()
        result = 0

Public Function FindFirstHeader(ByVal filename As String) As String
        Try
            If My.Computer.FileSystem.FileExists(filename) Then
                'OpenFile
                IO.File.Copy(filename, files, False)
                Dim reader As New FileStream(files, FileMode.Open, FileAccess.Read)
                FileOpen(1, files, OpenMode.Input)
                While Not EOF(1)
                    reader.Seek(pos, SeekOrigin.Begin)
                    reader.Read(headerarray, count, 4)
                    If ((headerarray(0) = 255) And ((headerarray(1) = 251) Or (headerarray(1) = 250))) Then
                        result = pos
                    Else
                        pos = pos + 1
                    End If
                End While
                reader.Close()
            Else
                MsgBox("File Error")
            End If
           
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        Return result


    End Function

推荐答案

您正在混合.NET-样式文件操作( FileStream ),具有VB6样式的文件操作( FileOpen EOF FreeFile )。



您还试图打开两次相同的文件。当您调用 FileOpen 时,您没有指定共享选项,因此它使用默认值: LockReadWrite 。这很可能会阻止文件第二次打开。



切换到使用.NET文件API - 它更干净。



您还应该打开 Option Strict ,它会警告您有关错误的声明,例如声明您的函数返回字符串当它实际返回整数时。



这样的东西应该有效:

You're mixing .NET-style file operations (FileStream) with VB6-style file operations (FileOpen, EOF, FreeFile).

You're also trying to open the same file twice. When you call FileOpen, you've not specified a sharing option, so it uses the default: LockReadWrite. That will most likely prevent the file from being opened the second time.

Switch to using the .NET file API - it's much cleaner.

You should also turn on Option Strict, which will warn you about errors such as declaring that your function returns a String when it actually returns an Integer.

Something like this should work:
Public Function FindFirstHeader(ByVal filename As String) As Integer
    If IO.File.Exists(filename) Then
        Using stream As New FileStream(filename, FileMode.Open, FileAccess.Read)
            Dim headerarray(0 To 3) As Byte
            Dim pos As Integer = 0
            Dim count As Integer = 4
            While count = 4
                reader.Seek(pos, SeekOrigin.Begin)
                count = reader.Read(headerarray, 0, 4)
                If count = 4 AndAlso headerarray(0) = 255 AndAlso (headerarray(1) = 251 OrElse headerarray(1) = 250) Then
                    Return pos
                End If
                
                pos = pos + 1
            End While
        End Using
    End If
    
    Return -1
End Function


这篇关于如何在VB.NET中找到头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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