Reading File From Network Location

不想你离开。 提交于 2020-05-13 14:16:05

问题


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

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 .

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

Directory Structure is Like that

\\10.0.2.20\Smartjobs\Eto\

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


回答1:


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.

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.




回答2:


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.

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




回答3:


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

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

You specify the name of the file on the network just like you do from anywhere else:

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



来源:https://stackoverflow.com/questions/10684879/reading-file-from-network-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!