简体   繁体   English

检查是否存在来自远程服务器的文件

[英]Check if file from remote server exists

i have an application which requires access permission to a file on remote server. 我有一个需要远程服务器上文件访问权限的应用程序。

My app is in Server A, and the file i want to access is in Server B. These 2 servers are in the same domain. 我的应用程序在服务器A中,我要访问的文件在服务器B中。这2台服务器在同一域中。

I created a virtual directory in Server A for the directory in Server B. The name of virtual directory is FolderFromServerB and its path is \\ServerB\\Folder. 我在服务器A中为服务器B中的目录创建了一个虚拟目录。虚拟目录的名称为FolderFromServerB,其路径为\\ ServerB \\ Folder。 I use a user for auth, and when i test the connection in IIS it says all is OK. 我使用一个用户进行身份验证,当我在IIS中测试连接时,它说一切正常。

Also, when i put an anchor tag in a test file like below, i can access the file and the content is shown in the page: 另外,当我将锚标记放在如下所示的测试文件中时,我可以访问该文件,并且内容显示在页面中:

<a href="FolderFromServerB/test.txt">Test file</a> -->  **This works**

But my problem is when i use code in order to if that file exists or not, it always returns with False. 但是我的问题是,当我使用代码来确定该文件是否存在时,它总是返回False。 My code is like below: 我的代码如下:

FileInfo fi = new FileInfo(@"\\FolderFromServerB/test.txt"); FileInfo fi =新的FileInfo(@“ \\ FolderFromServerB / test.txt”); --> This doesn't work -> 这不起作用

Response.Write(fi.Exists); Response.Write(fi.Exists); --> This always 'False' ->这总是'False'

I granted 'Full Control' permission to my user& NETWORK SERVICE & Everyone & Administratos in Server B but i didnt work neither. 我向服务器B中的用户&网络服务&每个人&管理员授予了'完全控制'权限,但是我都没有工作。

How can i make it work? 我该如何运作?

It was working last week. 上周在工作。 I guess the server updated itself and some updates made that occur, but i couldn't find any workaround. 我猜服务器已经自我更新,并且发生了一些更新,但是我找不到任何解决方法。 Im so desperate now and i have to change all of my code and spend much time to make it work. 我现在非常绝望,我必须更改所有代码并花很多时间才能使它工作。

I found the workaround that is in web.config : 我发现了web.config中的解决方法:

<identity impersonate="true"  userName="{domain}\{username}" password="{password}"/>

I used File.Exist() for a few months, but then suddenly it was gone and didnt work, and i dont know why. 我使用File.Exist()了几个月,但是突然间它消失了而且没有用,我也不知道为什么。 But it is the solution above. 但这是上面的解决方案。

Your code does not work because the current execution folder of an ASP.Net application is not the folder of you application, but c:\\windows\\system32 . 您的代码不起作用,因为ASP.Net应用程序的当前执行文件夹不是您应用程序的文件夹,而是c:\\windows\\system32

When you create the FileInfo object, you will try to read c:\\windows\\system32\\FolderFromServerB\\test.txt . 创建FileInfo对象时,将尝试读取c:\\windows\\system32\\FolderFromServerB\\test.txt

The <a href="FolderFromServerB/test.txt"> works because the link will be relative to the current page (it won't works if the page is in another directory). <a href="FolderFromServerB/test.txt">有效,是因为该链接是相对于当前页面的链接(如果该页面位于另一个目录中,则该链接将<a href="FolderFromServerB/test.txt"> )。

If the file you are looking for is under your application directory, you can convert a virtual to a physical path using : 如果要查找的文件位于应用程序目录下,则可以使用以下命令将虚拟路径转换为物理路径:

string actualFilePath = HttpContext.Current.Server.MapPath("~/FolderFromServerB/test.txt");
FileInfo fi = new FileInfo(actualFilePath);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM