Professional IronPython


Interacting with the Environment



Download 20,35 Mb.
Pdf ko'rish
bet232/453
Sana01.07.2022
Hajmi20,35 Mb.
#723045
1   ...   228   229   230   231   232   233   234   235   ...   453
Interacting with the Environment
 

 
217
LISTINg 10-9: 
 Setting an environment variable using the .NET method
# Obtain access to Environment class properties.
from System import Environment
# Obtain all of the Environment class methods.
from System.Environment import *
# Import the EnvironmentVariableTarget enumeration.
from System import EnvironmentVariableTarget
# Create a temporary process environment variable.
SetEnvironmentVariable(‘MyVar’, ‘Hello’) 
print ‘MyVar =’, GetEnvironmentVariable(‘MyVar’)
# Create a permanent user environment variable.
SetEnvironmentVariable(‘Var2’, ‘Goodbye’, EnvironmentVariableTarget.User)
print ‘Var2 =’, GetEnvironmentVariable(‘Var2’) 
print ‘Var2 =’, GetEnvironmentVariable(‘Var2’, EnvironmentVariableTarget.User)
raw_input(‘\nOpen the Environment Variables dialog box...’)
# Delete the temporary and permanent variables. 
print ‘\nDeleting the variables...’
SetEnvironmentVariable(‘MyVar’, None)
SetEnvironmentVariable(‘Var2’, None, EnvironmentVariableTarget.User) 
print ‘MyVar =’, GetEnvironmentVariable(‘MyVar’) 
print ‘Var2 =’, GetEnvironmentVariable(‘Var2’, EnvironmentVariableTarget.User)
# Pause after the debug session.
raw_input(‘\nPress any key to continue...’)
The example begins with the usual assembly imports. It then creates a new environment variable 
using the 
SetEnvironmentVariable()
method. If you call 
SetEnvironmentVariable()
without 
specifying a particular level, then the .NET Framework creates a temporary process environment 
variable that only lasts for the current session.
The next step creates a permanent user environment variable. In this case, you must supply an 
EnvironmentVariableTarget
enumeration value as the third argument. This portion of the example 
also demonstrates something interesting. If you create a new permanent environment variable in 
a process, the .NET Framework won’t update that process (or any other process for that matter). 
Consequently, the first call to 
GetEnvironmentVariable()
fails, as shown in Figure 10-14.
To see the environment variable, you must either restart the process or you must call 
GetEnvironmentVariable()
with an 
EnvironmentVariableTarget
enumeration value. As a result, 
the second call succeeds. At this point, the example pauses so you can open the Environment Variables 
dialog box and see for yourself that the environment variable actually does exist as a permanent value.
Deleting an environment variable is as simple as setting it to 
None
using the 
SetEnvironmentVariable()
method. However, you need to delete permanent environment variables 
by including the 
EnvironmentVariableTarget
enumeration value, or the .NET Framework won’t 
delete it. Unlike the Python method, you won’t get an error when checking for environment variables 
that don’t exist using the .NET method. Instead, you’ll get a value of 
None
, as shown in Figure 10-14.
548592c10.indd 217
2/24/10 12:48:33 PM
www.finebook.ir



Download 20,35 Mb.

Do'stlaringiz bilan baham:
1   ...   228   229   230   231   232   233   234   235   ...   453




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish