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:\
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With