PDA

View Full Version : FileSystemObject in ASP.NET?


BarrMan
09-29-2007, 03:05 PM
Hey.
I'm trying to use FileSystemObject in ASP.NET using C# but I have no idea how it works and I've searched everysite I know for it but I got no information about it.

Is there another way of accessing files in ASP.NET using C#? Is the FileSystemObject an old fassioned way of doing it?

Thanks.

SouthwaterDave
09-29-2007, 04:36 PM
The FileSystemObject is a COM class so it is not the best option for use in .NET.

I use classes from the System.IO namespace. The Directory, File and Path classes are particularly useful. They have quite a few static methods so you often don't have to create an instance to use the class, e.g.,

string xxx = File.ReadAllText("C:\\Somewhere\\Something.txt");

BarrMan
10-01-2007, 12:59 AM
Umm.. I've tried that but it says that there isn't any type named file.

How can I do something like
For Each Item In Folder.Files
End For

But in ASP.NET using C#?

SouthwaterDave
10-01-2007, 10:57 AM
You need to import the System.IO namespace in your web page, e.g.,

<%@ImportNamespace="System.IO" %>

or to name it in your web.config, e.g.,

<configuration>
<system.web>
<pages>
<namespaces>
<add namespace="System.IO"/>
</namespaces>
</pages>
</system.web>
</configuration>

And the code to enumerate files in a folder:


DirectoryInfo dir = newDirectoryInfo(path);

foreach (FileInfo f in dir.GetFiles("*")) {
string name = f.Name;
long size = f.Length;
DateTime creationTime = f.CreationTime;
}

If you are using Visual Studio or Visual Web Developer just search for Basic File I/O in the Help pages for more information.

BarrMan
10-01-2007, 01:26 PM
Thanks alot! That makes sense! I'll try that :)