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:\