简体   繁体   中英

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.

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. I use a user for auth, and when i test the connection in IIS it says all is OK.

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. My code is like below:

FileInfo fi = new FileInfo(@"\\FolderFromServerB/test.txt"); --> This doesn't work

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

I granted 'Full Control' permission to my user& NETWORK SERVICE & Everyone & Administratos in Server B but i didnt work neither.

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 :

<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. 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 .

When you create the FileInfo object, you will try to read 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).

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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