从VB 6到vb.net的GetfileContent函数 [英] GetfileContent Function from VB 6 to vb.net

查看:87
本文介绍了从VB 6到vb.net的GetfileContent函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将VB6项目升级到VB.net并且 我坚持最后一个功能。因为它必须能够处理所有文件类型,所以它以二进制形式读取,但是以字符串形式写入(我认为)。我已经尝试使用MS网站上的2个示例进行二进制读取
,但一直不成功。它应该很简单,但我的思绪已经消失了。本$ S.

I am upgrading a VB6 project to VB.net and  am stuck on this last function. As this must be able to handle all file types it is read in binary but gets written as string(I think). I have tried using the 2 examples from the MS website for binary reading but have been unsuccesful. It should be simple but my mind has gone Homer S.

 私有函数GetFileContents(ByVal strPath As String)作为字符串

    Dim strBuff As String

      打开strPath For Binary作为#1

        strBuff =空格(LOF(1))

   获取#1 ,, strBuff

   关闭#1

        GetFileContents = strBuff

   退出功能

    ERR_HANDLER:

          MsgBox(Err.Description)

          Err.Clear()

    结束函数

 Private Function GetFileContents(ByVal strPath As String) As String
    Dim strBuff As String
       Open strPath For Binary As #1
        strBuff = Space(LOF(1))
    Get #1, , strBuff
    Close #1
        GetFileContents = strBuff
    Exit Function
    ERR_HANDLER:
          MsgBox(Err.Description)
          Err.Clear()
     End Function

谢谢

推荐答案


我正在将VB6项目升级到VB.net并且 我坚持最后一个功能。因为它必须能够处理所有文件类型,所以它以二进制形式读取,但是被写为字符串(我认为)。

I am upgrading a VB6 project to VB.net and  am stuck on this last function. As this must be able to handle all file types it is read in binary but gets written as string(I think).

该代码正在读取文件使用获取文件中所有字节的技术进入字符串缓冲区,绕过对文本文件的任何特殊处理。  字符串缓冲区可能需要特殊处理 - 有可能代码一次检查一个字符
,可能会将每个字符转换为整数或字节。 

That code is reading a file into a string buffer using a technique that gets all the bytes in the file, bypassing any special processing for a text file.   The string buffer probably requires special processing - there is likely code that examines it one character at a time, possibly converting each character into an Integer or Byte. 

这不会在.Net中起作用,因为字符串始终是Unicode。  您可以获取一个字节数组,并处理每个字节,而不是每个字符。 相当于ReadAllBytes。 请参阅:
$
https://docs.microsoft.com/en-us/dotnet/visual-basic/developing-apps/programming/drives-directories-files/how-to-read-from-二进制文件

That won't work in .Net becasue a string is always Unicode.   You can get an array of bytes instead, and process each byte, instead of each character.  The equivalent is probably ReadAllBytes.  See:
https://docs.microsoft.com/en-us/dotnet/visual-basic/developing-apps/programming/drives-directories-files/how-to-read-from-binary-files

如果该文件的一部分确实需要是一个字符串,您可以将字节数组(或部分)转换为字符串:

https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/strings/how-to-convert-an-array-of-bytes -into-a-string

If part of that file really needs to be a string you can convert a byte array (or part) into a string:
https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/strings/how-to-convert-an-array-of-bytes-into-a-string


这篇关于从VB 6到vb.net的GetfileContent函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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