7

I am trying to get a list of files from a server via ASP.NET. I have this code, which gets a list of files from a folder on my computer, now what I am trying to do is get files from an actual server, I have searched around for this, but found everything super complicated. If anyone can help me out or point me in the direction I want to do, that would be great.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using FTPProject.Models;

namespace FTPProject.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Title = "Home Page";
            var uploadedFiles = new List<UploadedFile>();

            var files = Directory.GetFiles(Server.MapPath("~/UploadedFiles"));

            foreach (var file in files)
            {
                var fileInfo = new FileInfo(file);

                var uploadedFile = new UploadedFile() { Name = Path.GetFileName(file) };
                uploadedFile.Size = fileInfo.Length;

                uploadedFile.Path = ("~/UploadedFiles/") + Path.GetFileName(file);
                uploadedFiles.Add(uploadedFile);
            }

            return View(uploadedFiles);
        }
    }
}

UPDATE

I have tried the following:

in my Web.Config:

Added this:

<appSettings>
    <add key="myPath" value="D:\Folder\PDF" />
  </appSettings>

and changed this in the controller:

var myPath = WebConfigurationManager.AppSettings["myPath"];

var files = Directory.GetFiles(Server.MapPath(myPath));

When I run this code, I get this error:

An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code

Additional information: 'D:\Folder\PDF' is a physical path, but a virtual path was expected.

NOTE: My application is not on the same server as the D:\ but I need to get a list of files from D:\

mason
  • 28,517
  • 9
  • 66
  • 106
user979331
  • 10,209
  • 56
  • 186
  • 339
  • A server where? On your network? On the internet somewhere? What's it's name/ip/server OS? Need more info. – Dave Zych Aug 28 '15 at 18:33
  • possible duplicate of [Using Directory.GetFiles in my .net website](http://stackoverflow.com/questions/15979037/using-directory-getfiles-in-my-net-website) – MethodMan Aug 28 '15 at 18:36
  • You have posted some code, what doesn't work about it when you deploy it to the server? – mason Aug 28 '15 at 18:42
  • A server on my network...looks like this \\EXAMPLE\EXAMPLE\Directory – user979331 Aug 28 '15 at 18:48
  • So you're getting an error? What's the error? By the way, make sure you use the @mason syntax when replying, otherwise I won't be notified. – mason Aug 28 '15 at 19:57
  • I see you've added a bounty to your question, but you still need to address the questions I've raised. You may be getting an HttpException, but you need to look at the InnerException property and provide the relevant details from it. – mason Aug 31 '15 at 17:18
  • @mason `An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code Additional information: 'D:\Folder\PDF' is a physical path, but a virtual path was expected.` – user979331 Aug 31 '15 at 17:21
  • @user979331 Edit that into your question. Vital info should be in the question itself, not a comment. – mason Aug 31 '15 at 17:23

2 Answers2

14

Server.MapPath takes virtual path, ex: ~/Folder/file.ext. So you can't pass in a physical path like D:\Folder\PDF to it.

The syntax for accessing a remote file system is different, you need a UNC file path. In your config file, myPath should be like \\servername\d$\Folder\PDF, and you won't need any calls to Server.MapPath and the user your site process is running as should be an admin on the server you're accessing.

Or you can specifically share out the folder and give permissions to the account your web server runs as, then you don't need admin privileges (this is more secure). Then the UNC path is like \\servername\sharename.

mason
  • 28,517
  • 9
  • 66
  • 106
  • I changed the `myPath` attribute in web.config to `` and got this error `Additional information: Could not find a part of the path 'c:\users\me\documents\visual studio 2013\Projects\FTPProject\FTPProject\servname\d$\Folder\PTD'.` – user979331 Aug 31 '15 at 17:34
  • @user979331 Did you make sure to remove `Server.MapPath`? – mason Aug 31 '15 at 18:08
  • 1
    Is SERVERNAME your current computer ? If so, you don't need the UNC. A local path (and of course no Server.MapPath !) would be enough. If not, caution, Directory.GetFiles may be not enough : you will probably have to handle remote authentication to UNC ! – AFract Sep 02 '15 at 07:20
1

Here is a reference: Server.MapPath("."), Server.MapPath("~"), Server.MapPath(@"\"), Server.MapPath("/"). What is the difference?

Server.MapPath specifies the relative or virtual path to map to a physical directory.

Server.MapPath(".")1 returns the current physical directory of the file (e.g. aspx) being executed
Server.MapPath("..") returns the parent directory
Server.MapPath("~") returns the physical path to the root of the application
Server.MapPath("/") returns the physical path to the root of the domain name (is not necessarily the same as the root of the application)

An example:

Let's say you pointed a web site application (http://www.example.com/) to

C:\Inetpub\wwwroot

and installed your shop application (sub web as virtual directory in IIS, marked as application) in

D:\WebApps\shop

For example, if you call Server.MapPath in following request:

http://www.example.com/shop/products/GetProduct.aspx?id=2342

then:

Server.MapPath(".")1 returns D:\WebApps\shop\products
Server.MapPath("..") returns D:\WebApps\shop
Server.MapPath("~") returns D:\WebApps\shop
Server.MapPath("/") returns C:\Inetpub\wwwroot
Server.MapPath("/shop") returns D:\WebApps\shop

If Path starts with either a forward (/) or backward slash (), the MapPath method returns a path as if Path were a full, virtual path.

If Path doesn't start with a slash, the MapPath method returns a path relative to the directory of the request being processed.

Community
  • 1
  • 1
jdaval
  • 520
  • 3
  • 10