C# Web App Question
I want to show the contents of a folder in my C# web app. How do I get this done? I know how too in windows apps, but Im working on a web app here.
Thanks
Updated:
Sorry guys, I have two servers, A & B. A has the web app and on server B, I want have the file folder that I want to place files in, excel files, etc. My web app is going to proce开发者_如何转开发ss the files and I want the ability to show within the app what files are in the folder on server B. Does this help?
Assuming that you want to list content of a web-folder on the server
Since I don't know if you are doing this in ASP.NET MVC or ASP.NET WebForms I will just use Response.Write
to print it out on the page.
What you need to do is pretty much like you do in a windows app, you use DirectoryInfo
to get a file list and then you can get the file info out of that.
Remember that the User running the web-server / web-app need to have permissions to read and list files from that folder.
var files = new DirectoryInfo(@"C:\inetpub\wwwroot\mywebapp\folder1").GetFiles();
foreach(var file in files)
{
Response.WriteLine(file.FullName);
}
Listing files on the clients machine inside your webb-application
Using Javascript to do this is Impossible, since Javascript lives inside the browser and has no way of contacting content outside of the scope of the web-browser. There are however alternatives to this, you can use Java Applets, Flash, Silverlight, XBAP or ActiveX to read files from the client machine. However this must then be allowed by the user and the user needs to have the specific runtimes.
And you most certainly cannot read files on the client machine using C# because your Web Application ( if it is not XBAP ) lives and runs on the server machine and does not have a connection to the client computer besides the stateless communication over HTTP.
Reading files from another server
If the servers are on the same network and assuming both are running windows you could just mount a network volume and read the files as show in the first section of my post. However if they are not, you can either use FTP with C# to access a file list, check out FtpWebRequest for that.
Or another option is to put a WebService on the other server and have it return file lists to you. You can read about begining Windows Communication Foundation implementations over at this blog.
精彩评论