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 were trying to read or write. Setting
the permissions depends on our context. Here's a couple of possible scenarios:
If youre running the page using Cassini or Visual Web Developers integrated
web server, the code will run under the credentials of your user account, so
it will inherit all your permissions. For example, if youre a computer admin-
istrator, then your page will be able to access any resource on your computer.
If youre 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 applications virtual directory.
On your own machine, youll need to set special permissions only if you use IIS,
and you want to write in a folder thats not part of an existing IIS application.
If youre in this situation, read on. Otherwise, feel free to skip to the next section,
in which well create within your applications 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, Ill assume its at C:\WritingTest.
2. In Windows XP, simple file sharing is enabled by default. This hides the
Security tab youll 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
Figure 14.1. Disabling simple file sharing
In the View tab, uncheck Use simple file sharing (Recommended) (as Fig-
ure 14.1 indicates, this should be the last option on the list).
3. Open the C: drive with the Windows Explorer (not the IIS control panel),
right-click on the WritingText directory and select Properties.
4.
Select the Security tab.
5.
Add the ASPNET account to the Group or user names list by clicking Add,
and typing it into the Select Users or Groups dialog as shown in Figure 14.2.
A new entry called ASP.NET Machine Account (machinename\ASPNET) will be
added to the list.
574
Chapter 14: Working with Files and Email
Figure 14.2. Adding the ASPNET account
6.
Select the new user in the list, and click on the Write checkbox under Allow
in the permissions list, as shown in Figure 14.3.
7.
Click OK.
Figure 14.3. Giving write access to ASPNET
575
Setting Up Security

Get Build Your Own ASP.NET 2.0 Web Site Using C# & VB, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.