“filestream”类型的值无法转换为“字符串”。 [英] Value of type 'filestream' cannot be converted to a 'string'.

查看:123
本文介绍了“filestream”类型的值无法转换为“字符串”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一些FileSystemWatcher1_Changed的代码。





I have some code here for FileSystemWatcher1_Changed.


Private Sub FileSystemWatcher1_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed
        Dim path As FileStream = Nothing
        Try

            path = File.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None)
            path.Close()
        Catch ex As Exception

            Dim sample As String
            sample = md5_hash(path)


            Using f As System.IO.FileStream = System.IO.File.OpenRead("viruslist.txt")
                Using s As System.IO.StreamReader = New System.IO.StreamReader(f)
                    While Not s.EndOfStream
                        Dim line As String = s.ReadLine

                        If (line = sample) Then
                            Detect.ShowDialog()
                        End If
                    End While
                End Using
            End Using
        End Try
    End Sub







在行




In line

sample = md5_hash(path)

中,'path'表示'FileStream'的值类型无法转换为'字符串'。





有没有办法解决这个问题?



我尝试过:



研究这个问题。





将'path'声明为数组(出于某种原因,我认为它可能会有所作为。)

, 'path' is saying that Value type of 'FileStream' cannot be converted to 'String'.


Is there any way of fixing this?

What I have tried:

Researching this problem.


Declaring 'path' as an array (for some reason I thought it might do something.)

推荐答案

FileSystemWatcher1_Changed 为您提供路径:参数 e 的类型为 FileSystemEventArgs ,其中包含一个属性 FullPath



所以:



FileSystemWatcher1_Changed provides you with a path: the parameter e is of type FileSystemEventArgs which has a property FullPath.

So:

path = e.FullPath


回到上一个问题:有没有办法在变量打开时为变量分配文件路径而不使用openfiledialog? [ ^ ]并阅读答案。

然后看看你的代码,开始思考。目前,您正在猜测该做什么 - 这不是一个可行的开发策略。

看看你告诉我们的代码!它甚至不会因为这么多原因而编译它变得愚蠢:笑:



到底是什么让你认为 - 即使那段代码奇迹般地开始工作 - 所有更改的文件都将包含基于文本的数据(即适合字符串的数据)?计算机上的数据很少是文本:只有人类可读的东西。所以你的MD5(不推荐用于新项目并且还没有推出半年)的方法不应该期望一个字符串,它应该期望一个字节数组。



说真的:停止猜测;开始思考。这不是一个很难的代码,但你做的最大的事情就是让它变得尽可能复杂!
Go back to your previous question: Is there a way to assign a file path to a variable when its opened without using openfiledialog?[^] and read the answer.
Then look at your code, and start thinking. At the moment, you are guessing what to do - and that isn't a viable development strategy.
Look at the code you show us! It won't even compile for so many reasons that it's getting silly :laugh:

And what on earth makes you assume that - even if that code did miraculously start working - all files that change will contain text-based data (i.e. data that will fit in a string)? Very little data on a computer is text: only the human readable stuff. So your MD5 (which is not recommended for new projects anyway and hasn't been for half a dozen years) method should not expect a string, it should expect an array of bytes.

Seriously: stop guessing; start thinking. This isn't difficult code, but you are doing your damnedest to make it as complicated as possible!


这篇关于“filestream”类型的值无法转换为“字符串”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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