C# 从网络位置读取文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10684879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:48:56  来源:igfitidea点击:

Reading File From Network Location

c#asp.net.netnetwork-share

提问by kuldeep verma

I am having Bunch of Files in A folder which is shared on Network Drive . I am trying to Access those Files into my Code . But It is giving an error:

我在网络驱动器上共享的文件夹中有一堆文件。我正在尝试将这些文件访问到我的代码中。但它给出了一个错误:

System.IO.DirectoryNotFoundException was unhandled by user code

System.IO.DirectoryNotFoundException 未被用户代码处理

Fname = txtwbs.Text;
DirectoryInfo objDir = new DirectoryInfo("Y:\");
_xmlpath = objDir + "\" + Fname + "\" + Fname + ".xml";
if (File.Exists(_xmlpath ))
{
    reader(_xmlpath);
}

I have Also used:

我也用过:

file = fopen("\\10.0.2.20\smartjobs\Eto\"+Fname);   

I am Able to Read File from My Local PC But it is giving Exception Only for Network Location .Please let me know how can I read File From Network Shared Location .

我能够从我的本地 PC 读取文件,但它仅针对网络位置给出异常。请告诉我如何从网络共享位置读取文件。

And Also How Can I Make A tree view of Folders into Asp.net Web Application .

以及如何将文件夹的树视图制作成 Asp.net Web 应用程序。

Directory Structure is Like that

目录结构就是这样

\10.0.2.20\Smartjobs\Eto\

this is Parent Directory It is congaing Nos of Folder having XML Documents.

这是父目录,它包含多个具有 XML 文档的文件夹。

回答by Niranjan Singh

You need to specify that the ASP.net page run as a certain user with access to the file. Then, you need to enable impersonationin your web.configfile in order for ASP.net to actually access the file as that user.

您需要指定 ASP.net 页面以有权访问该文件的特定用户身份运行。然后,您需要impersonation在您的web.config文件中启用,以便 ASP.net 实际访问该文件user

Your Y drive is a mapped network drive. You need to use the network url eg \\server\Smartjobs\Eto\xyz.xml

您的 Y 驱动器是映射的网络驱动器。您需要使用网络网址,例如\\server\Smartjobs\Eto\xyz.xml

You specify the nameof the file on the networkjust like you do from anywhere else:

您可以像在其他任何地方一样指定name文件的network:

Dim myStream As IO.FileStream = IO.File.Open("\myserver\myshare\myfile", IO.FileMode.Open)
Dim myBytes As Byte()
myStream.Read(myBytes, 0, numberOfBytesToRead)

More reference:
Unable to List File or Directory Contents on ASP.NET Page using Shared Drive
Using file on network via IIS

更多参考:
Unable to List File or Directory Contents on ASP.NET Page using Shared Drive
Using file on network via IIS

回答by bhavesh lad

In asp.net, you cannot access network folder directly because asp.net runs under anonymous user account, that account does not have access to that location.

在 asp.net 中,您不能直接访问网络文件夹,因为 asp.net 在匿名用户帐户下运行,该帐户无权访问该位置。

You can give rights to "Everyone" in that shared location and see if it is working. However this is not advisable.

您可以在该共享位置向“所有人”授予权限,看看它是否有效。然而,这是不可取的。

Alternativly You may have to do impersonation in asp.net code when accessing network location. You will have to do implersonation with the user who has access to that shared location.

或者,您可能需要在访问网络位置时在 asp.net 代码中进行模拟。您必须模拟有权访问该共享位置的用户。

回答by Aristos

You may have map the shared directory as a user, but you forget that the asp.net is running under the account of the pool, and there you do not have connect the y:\ with the shared directory.

您可能以用户身份映射了共享目录,但您忘记了 asp.net 正在池的帐户下运行,并且您没有将 y:\ 与共享目录连接。

The next think that you can do is to direct try to connect via the network shared name, eg: \\SharedCom\fulldir\file.xml

接下来你可以做的是直接尝试通过网络共享名称连接,例如: \\SharedCom\fulldir\file.xml