03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

394 ❘ CHAPTER 17 Configuration and Resources<br />

File Not Found<br />

Configuration and resource files store information in files with specific formats.<br />

This chapter doesn’t discuss more general uses of files. Reading and writing files,<br />

manipulating the filesystem, working with databases (which may be stored in files),<br />

and working with XML files are all topics covered by later chapters.<br />

Environment Variables<br />

Environment variables give information about the operating system environment in which the program<br />

runs. They hold information such as the computer’s name, the user’s login name, the location<br />

of the system’s temporary directory, the number of processors the system has, and the program’s<br />

current working directory. You can also store configuration information in environment variables<br />

for your programs to use.<br />

There are three types of environment variables that apply at the system, user, and process levels.<br />

System-level variables apply to all processes started on the system; user-level variables apply to<br />

processes started by a particular user; and process-level variables apply to a particular process<br />

and any other processes that it starts.<br />

Environment variables are loaded when a process starts, and they are inherited by any process launched<br />

by the initial process. During program development, variables are loaded when you start Visual Studio.<br />

Their values are inherited by the program you are working on when you start it. If you make changes<br />

to the system’s environment variables, you need to close and reopen Visual Studio before your program<br />

can see the changes.<br />

A program can also create temporary process-level variables that are inherited by any processes you<br />

launch. Those values disappear when the original process ends.<br />

A <strong>C#</strong> program can use the System.Environment class to read and write environment values. Before<br />

you learn how to use that class, however, you should learn how the operating system sets environment<br />

variables’ values.<br />

Setting Environment Variables<br />

Environment variables are normally set on a systemwide basis before the program begins. In older<br />

operating systems, batch files such as autoexec.bat set these values. More recent systems provide<br />

Control Panel tools to set environment variables.<br />

Newer systems also use an autoexec.nt file to set environment variables that apply only to commandline<br />

(console) applications, so they don’t affect GUI applications. Sometimes, you can use this fact to<br />

your advantage by giving different kinds of applications different environment settings.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!