StreamReader无法在一个用户/ PC的网络位置中找到文件。 [英] StreamReader unable to find file in network location for one user/PC only out of many.

查看:60
本文介绍了StreamReader无法在一个用户/ PC的网络位置中找到文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TLDR; StreamReader可以为许多程序用户访问文件,但不适用于一个用户/机器。确认用户具有对文件的正确访问权限。


我有一个程序正在从存储在网络共享中的已保存文本文件中解析数据。大约20个用户/ PC能够毫无问题地使用该程序,但是一个用户无法执行任务,收到异常:

 System.IO.FileNotFoundException:找不到文件
'\\\\\\\\\\\\\\\\\\\\\\\\\

(显然不是实际路径,但逻辑保持不变)


文件存在,这个特殊用户可以手动导航到共享/文件夹并通过Windows资源管理器打开文件,因此他具有适当的访问权限。这是相关的代码块:

 Dim lines As New List(Of String)
Dim sr As New StreamReader(strNEDir& sFilename)
而不是sr.EndOfStream'读取每一行直到结束
Dim line As String = sr.ReadLine
For Each row As DataGridViewRow In Me.gridNE.Rows
如果Mid(line,2,8)= Mid(row.Cells(" colTask​​")。Value.ToString,1,8)然后'检查line = task
如果不是lines.Contains(然后
lines.Add(line)'如果是的话,添加到列表发送显示
结束如果
结束如果
下一个
结束而
sr.Close()

尝试使用StreamReader打开文件时抛出异常(粗体线,我已经确认'strNEDir& sFilename'i正确填充正确的完整路径 - 这些变量填充在其他地方)。这显然是一些专门针对这个用户或机器的
设置,任何人都有任何线索可能是什么?我在这里缺少什么?






解决方案

对代码进行一点重组。

 Dim pthToFile As字符串
pthToFile = IO.Path.Combine(strNEDir,sFilename)
如果IO.File.Exists(pthToFile)那么
Dim sr As New StreamReader(pthToFile)
'其余的你的代码
否则
'调试代码在这里
结束如果


TLDR; File accessible by StreamReader for many users of program, but does not work for one user/machine. Confirmed that user has proper access to file.

I have a program that is parsing data from a saved text file stored on a network share. Roughly 20 users/PCs are able to use this program without issue, however one user is unable to perform the task, receiving an exception:

System.IO.FileNotFoundException: Could not find file
'\\server\path\to\folder\file.G5246V00'.

(obviously not the actual path, but the logic remains the same)

The file exists, and this particular user can manually navigate to the share/folder and open the file through Windows Explorer, so he has proper access. Here's the relevant code block:

            Dim lines As New List(Of String)
            Dim sr As New StreamReader(strNEDir & sFilename)
            While Not sr.EndOfStream 'read each line until the end
                Dim line As String = sr.ReadLine
                For Each row As DataGridViewRow In Me.gridNE.Rows
                    If Mid(line, 2, 8) = Mid(row.Cells("colTask").Value.ToString, 1, 8) Then 'check if line = task
                        If Not lines.Contains(line) Then
                            lines.Add(line) 'if it does, add to list to send for display
                        End If
                    End If
                Next
            End While
            sr.Close()

The exception is thrown when trying to open the file with StreamReader (bolded line, I've confirmed that 'strNEDir & sFilename' is properly populated with the correct full path - those vars are populated elsewhere). This is clearly some setting pertaining specifically to this user or machine, anyone have any clue what it might be? What am I missing here?


解决方案

A little restructuring of your code.

        Dim pthToFile As String
        pthToFile = IO.Path.Combine(strNEDir, sFilename)
        If IO.File.Exists(pthToFile) Then
            Dim sr As New StreamReader(pthToFile)
            'rest of your code
        Else
            'debug code here
        End If


这篇关于StreamReader无法在一个用户/ PC的网络位置中找到文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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