Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Build Your Own ASP.NET 2.0 Web Site Using CSharp And VB (2006) [eng]-1.pdf
Скачиваний:
143
Добавлен:
16.08.2013
Размер:
15.69 Mб
Скачать

Chapter 14: Working with Files and Email

upload files from the client to the server

You may want to create an interface that allows staff from the Human Resources department to upload company documentation for reference by employees.

access directories and directory information

You may want to let the Human Resources department choose the drive to which staff will upload files. For instance, you may have one drive dedicated to spreadsheets, and another just for Word documents.

Once you have a firm grasp on the intricacies of working with text files and directory information, you’ll learn how to send email in ASP.NET using the System.Net.Mail namespace. We’ll finish the chapter with a quick introduction to serialization.

Writing and Reading Text Files

The System.IO namespace contains three different groups of classes:

classes for working with files

classes for working with streams

classes for working with directories

As we progress through this chapter, we’ll look at each of these groups. However, let’s begin by discussing the tasks of writing to and reading from text files with the aid of the classes that work with files and streams. These classes include:

File

contains methods for working with files

FileStream

represents a stream for reading and writing to files

StreamReader

reads characters from a text file

StreamWriter

writes characters to a text file

572

Setting Up Security

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 administrator, 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

Chapter 14: Working with Files and Email

Figure 14.1. Disabling simple file sharing

In the View tab, uncheck Use simple file sharing (Recommended) (as Figure 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

Setting Up Security

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