Path
contains methods for manipulating a file or directory
For the most part, we read from and write to text files by using the File class to
return a stream. If we want to write to a text file, we use the StreamWriter class;
conversely, we use the StreamReader class to read from a text file.
Setting Up Security
Before our ASP.NET page can read and write files on your hard disk, the ASP.NET
page must have permissions to access the file we’re trying to read or write. Setting
the permissions depends on our context. Here's a couple of possible scenarios:
❑
If you’re running the page using Cassini or Visual Web Developer’s integrated
web server, the code will run under the credentials of your user account, so
it will inherit all your permissions. For example, if you’re a computer admin-
istrator, then your page will be able to access any resource on your computer.
❑
If you’re running the page using IIS, the code will run under the credentials
of the ASPNET user account on your system. By default, this account has
access to any folder that is part of an IIS application.
Running Under IIS
The IIS scenario is particularly relevant because your web application will
run under IIS when hosted on a production server. Also of note is the fact
that, while you may fine-tune the permission rules on your development
machine, on a hosting server, you will probably not be allowed to access
folders outside your application’s virtual directory.
On your own machine, you’ll need to set special permissions only if you use IIS,
and you want to write in a folder that’s not part of an existing IIS application.
If you’re in this situation, read on. Otherwise, feel free to skip to the next section,
in which we’ll create within your application’s folder structure a file that will be
accessible under the default configuration of either IIS or Cassini.
1. Create a new folder called WritingTest somewhere on your disk. For the
purposes of this discussion, I’ll assume it’s at C:\WritingTest.
2. In Windows XP, simple file sharing is enabled by default. This hides the
Security tab you’ll need to select in Step 4, preventing you from granting web
applications write access to this directory. To disable simple file sharing,
open the Windows Control Panel and double-click the Folder Options icon.
573
Setting Up Security