欢迎来到淘文阁 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
淘文阁 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

    Windows脚本编程核心技术精解Chapter14.pdf

    • 资源ID:70332874       资源大小:203.01KB        全文页数:16页
    • 资源格式: PDF        下载积分:15金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要15金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    Windows脚本编程核心技术精解Chapter14.pdf

    Chapter 14Getting Access to the RegistryIn This Chapter?Find out why the Registry is so important and how to back up its contents?Find out how the Registry organizes the internal Windows settings intodifferent branches?Use script commands to read and write to the Registry,and learn how tosafely exchange the registered owner information?Store your own values into the Registry and delete Registry information youno longer need?Enumerate entire Registry branches and list all entries hidden inside a keyThe Windows Registry is the ultimate place to control Windows at a very granular level.Learn how to access and manipulate the Registry by script,and use a custom COM control to add Registry methods not foundin the standard WScript.Shellobject.Getting to Know the RegistryThe Registry is an incredible source of powerits here that Windows stores all its internal settings.You can control many Registry settingsthrough system dialog boxes,but many more Windows options are hidden and available only through direct Registry access.Lets take a look at the Registrys anatomy first.Then,Ill show you how toback up your Registry.Once you know the Registry better,its safe to usescripts to manipulate Registry entries and create your own dialog boxes for any Windows option missing in the official dialog boxes.Where does the Registry live?The Registry actually is a database.Its merely data storage,and itsorganized differently for Windows 9.x and Windows NT/2000.4684-8 ch14.f.qc 3/3/00 9:39 AM Page 407Registry on Windows 9.xOn Windows 9.x systems,the Registry consists of two filesUSER.DATandSYSTEM.DAT.They are stored in the Windows folder and marked as hidden.SYSTEM.DATcontains all common settings,and USER.DATcontains user-related information.If you have set up your Windows 9.x to support multiple user profiles,theRegistry still uses the central SYSTEM.DAT.For the user information,it usesindividual USER.DATfiles stored in the profiles subfolder.Registry on Windows NT/2000Windows NT/2000 organizes the Registry differently.Here,only users with Administrator or Power User membership have access to the files.The common machine-related information is stored in the subfolderSYSTEM32CONFIGand consists of these files:DEFAULTSAMSECURITYSOFTWARESYSTEMThe user-related information is stored in a file called NTUSER.DAT.These files are stored individually in a subfolder called PROFILES/username/.Accessing Registry contentYou dont need to care much about the physical location of your Registrydata.All Windows versions come with a special Registry Editor.Just chooseRunfrom your Start menu and enter REGEDIT.Dont change any Registry settings yet.The Registry Editor has direct access to the Registry,and any changes will be written to your“live”Registryimmediately,without a way to undo the changes.You may easily crash yourentire system or render it useless once you restart.The Registry Editor makes the Registry data visible and uses an Explorer-liketwo-pane view.In the left column,you see the Registry keys.In the right pane,you see the actual data contained in the selected key.Table 14-1 shows how the Registry splits up information into a number of mainkeys,the“handles.”These handles and their subkeys act like file system folders and share thesame icon,too.View Registry keys as information folders,and view the datain the right pane as information files.408Part IV:Accessing the Operating SystemII4684-8 ch14.f.qc 3/3/00 9:39 AM Page 408Table 14-1Main Windows Registry KeysMain KeyDescriptionHKEY_LOCAL_MACHINEHardware information and information that applies to allusers.HKEY_CURRENT_USERUser information for the currently logged-on user.It isactually a shortcut to HKEY_USERSusername.HKEY_USERSAccess to all user information(accesses all availableNTUSER.DAT).On Windows 9.x,you only see yourown user account.defaultrepresents the defaultuser account,which is used as long as nobody haslogged on.HKEY_CLASSES_ROOTShortcut to HKEY_CLASSES_ROOTSoftwareClasses.Contains information about registered filetypes and COM objects.HKEY_CURRENT_CONFIGMostly a shortcut into HKEY_LOCAL_MACHINESystemCurrent Control Set.HKEY_DYN_DATADynamic performance data only available on Windows 9.x.The right pane splits information up into a name and a value.The namerepresents an entry while the value represents its content.To stick to the file system analogy,view the name as filename and the value as file content.Each key has at least one name entry:(Standard).Backing up the RegistryBefore you start experimenting with the Registry,you should make sure to create a backup.Its your only safety net.If something goes wrong or you accidentally delete the wrong item,you can always restore your backup.Without a backup,you would be lost and would most likely need to reinstallyour complete system.Backing up the Registry is generally a good idea.Many Windows problemsoccur during software-or hardware-modification.With the help of a currentRegistry backup,you can undo those errors and revitalize your system in a matter of seconds.A Registry backup,therefore,is one of the mostimportant safeguards you can take advantage of.Backing up on Windows 95On your Windows CD,youll find the folder ERD.Copy it to your hard drive,then launch ERU.EXE.It will create a complete backup of important systemfiles,including the Registry.Dont use disks to store the backupthey aretoo small and wont save the Registry.Instead,store the backup somewhereon your hard drive.Chapter 14:Getting Access to the Registry409II4684-8 ch14.f.qc 3/3/00 9:39 AM Page 409To restore the Registry,you must start your computer in DOS mode.Holddown F8 while Windows 95 starts and choose Command Prompt Only fromthe hidden boot menu.Then,navigate to the folder in which you stored yourbackup using the CD.You cant call your backup set from outside the backup folder.You need tochange the current directory to the backup directory.In your backup folder,call ERD Enter.The backup will be restored.Backing up on Windows 98Windows 98 automatically backs up the Registry every day and maintains the last five copies.You can find them in the SYSBCKUPfolder inside yourWindows folder.To restore a backup,start your computer in DOS mode and call SCANREG/RESTORE.Windows checks the current Registry and then offers to restoreone of the five last backup sets.The Registry will constantly grow in size because deleted entries arent really deleted.They are just marked as“bad.”To re-create your Registry from scratch and discard any unused data,start your computer in DOS mode and use SCANREG/FIX.Backing up on Windows NTOn Windows NT,you can use the RDISK.EXEutility to create a rudimentaryemergency backup disk.It doesnt contain the complete Registry and willonly restore the most important keys.To create a full Registry backup,use the backup software provided withWindows NT.Make sure to select the option Backup Local Registry to include the Registry files.You must restore a Registry on the same system that you created the backup.Restoring it on a system with different hardware wont work.The easiest way to recover from Registry problems is the Last Known Goodconfiguration.If you select this option during boot time,Windows NT willautomatically use the last Registry that worked without errors.Backing up on Windows 2000Windows 2000 comes with a sophisticated backup program.It allows you to back up files,folders,and the system-relevant parts.The system backupincludes the Registry.Windows 2000 also includes a Last Known Good configuration you can use to quickly solve Registry problems.In addition,Windows 2000 sports manyother recovery options organized in its boot menu.Press F8 while Windows2000 offers its boot menu option on-screen.410Part IV:Accessing the Operating SystemII4684-8 ch14.f.qc 3/3/00 9:39 AM Page 410Exporting parts of the RegistryComplete Registry backups are necessary,but you dont need to back up the entire Registry every time you make changes.A much easier way is toexport part of the Registry.To do this,you select the branch you want tosave in the left Registry Editor pane.Then,you call Export Registry from the Registry menu.Now,you can export the selected branch or all of the Registry to a REGfile.REGfiles are plain text filesyou can open them in any text editor to viewthe saved settings.To restore the saved branch,all you need to do is open the REGfile.Itscontent will be merged into the Registry.REGfiles can only add entries.So,if you have made a backup and then addedkeys to the Registry,merging the backup wont remove the added keys.It willonly restore the backed-up entries.You can even restore REGfiles automatically You call REGEDIT/Sregfilename,for example,by script.Re-creating a Registry from scratchOn Windows 9.x,you have another option to restore the Registry.If you have exported the entire Registry as REGfile,you can rebuild the Registryusing this file.In DOS mode,enter REGEDIT/C regfilename.Recreating a Registry only works if the REGfile really contains the entirebackup.The/Coption doesnt merge information.Instead,the Registry willbe created based entirely on the information stored in the REGfile.Also,onold Windows 95 systems,due to an error in REGEDIT.EXE,the creationprocess fails.The re-creation process can take many hours.It doesnt have to be that slow,though.If you launch SMARTDRVbeforehand,the re-creation process onlytakes minutes.SMARTDRVactivates the old DOS-compliant hard drive cache.Without this cache,file operations in pure DOS mode are incredibly slow.Reading and Writing to the RegistryHow can you access Registry values by script?The WScript.Shellobjectoffers the methods needed,which are RegReadand RegWrite.Reading the Registered OwnerWhen you installed Windows,the installer prompted you for your name andorganization.This information is stored inside your Registry,and if you wantto change these entries,all thats needed is a little change to your Registry.Chapter 14:Getting Access to the Registry411II4684-8 ch14.f.qc 3/3/00 9:39 AM Page 411Figure 14-1:Read the Registered Owner information.Lets first see how to retrieve the information:14-1.VBSset wshshell=CreateObject(“WScript.Shell”)key=“HKLMSoftwareMicrosoftWindowsCurrentVersion”use this key for Win NT/2000 key=“HKLMSoftwareMicrosoftWindows NTCurrentVersion”owner=ReadKey(key&“RegisteredOwner”)org=ReadKey(key&“RegisteredOrganization”)msg=“Registered Owner:“&owner&vbCrmsg=msg&“Organization:“&orgMsgBox msg,vbInformationfunction ReadKey(key)on error resume nextReadKey=wshshell.RegRead(key)if not err.number=0 thenMsgBox“Couldnt open key“”&key&“”:“_&err.descriptionWScript.Quitend ifend functionThe script wraps the RegReadmethod as ReadKeyfunction.Why?BecauseRegReadraises an error if the key you are trying to access is missing or notaccessible.To prevent this,the function turns off error handling and checksfor errors.Windows 9.x and Windows NT/2000 use different Registries.Although theRegistries are not compatible,they have a very similar architecture.In theexample,replace Windows with Windows NT to adjust the script to WindowsNT/2000.You can also use the Windows version-checking methods from theprevious chapters to adjust the keys automatically.Changing Registry informationNow that you know where Windows stores the registration info,how can youchange the information?Use RegWrite.412Part IV:Accessing the Operating SystemII4684-8 ch14.f.qc 3/3/00 9:39 AM Page 412 14-2.VBSset wshshell=CreateObject(“WScript.Shell”)key=“HKLMSoftwareMicrosoftWindowsCurrentVersion”use this key for Win NT/2000 key=“HKLMSoftwareMicrosoftWindows NTCurrentVersion”ownerkey=key&“RegisteredOwner”orgkey=key&“RegisteredOrganization”owner=ReadKey(ownerkey)org=ReadKey(orgkey)newowner=InputBox(“Change owner information!”,owner)if newowner=vbEmpty then WScript.Quitwshshell.RegWrite ownerkey,newownerneworg=InputBox(“Change organization information!”,org)if neworg=vbEmpty then WScript.Quitwshshell.RegWrite orgkey,neworgMsgBox“Changed information”function ReadKey(key)on error resume nextReadKey=wshshell.RegRead(key)if not err.number=0 thenMsgBox“Couldnt open key“”&key&“”:“_&err.descriptionWScript.Quitend ifend functionThis time,an InputBoxdialog box presents you with the current Registryvalues and allows you to change the information.Right-click My Computer onthe desktop and choose Properties to verify that your changes really take effect.Tricks and tips around Registry editsRegReadand RegWriteare both pretty straightforward.However,there are some details you should know:IIf the key you supply ends with a,then RegReadreads the keys(Standard)value,and RegWritewrites the(Standard)value.IIf the key you supply doesnt end with a,then RegReaduses the lastpart of your key as entry name.For example,HKCR.vbswould try toaccess the entry.vbs.This would fail because.vbsis a subkey.Youwould have to access it through HKCR.vbs.IIn general,its best to stick to the file system analogy.If your key endswith a,it represents a“folder”(key).If it doesnt end with a,itrepresents a“file”(an entry in the right Registry Editor pane).Chapter 14:Getting Access to the Registry413II4684-8 ch14.f.qc 3/3/00 9:39 AM Page 413As a default,RegWritecreates new entries as String Value.If you want tocreate entries of different type,add the desired type according to Table 14-2.Table 14-2Registry Variable TypesEntry TypeDescriptionREG_SZString ValueREG_EXPAND_SZString ValueREG_DWORDInteger ValueREG_BINARYInteger ValueTo write a DWORDentry to the Registry,use the following syntax:wshshell.RegWrite key,value,REG_DWORD”.Deleting Registry valuesYou can even delete Registry information using RegDelete.For example,you could store your own data in the Registry and delete it on request:14-3.VBSset wshshell=CreateObject(“WScript.Shell”)mykey=“HKCUSoftwaremystoragecounter”read countercounter=ReadKey(mykey)use informationmsg=“You have launched this script“&counter&“times.Reset?”answer=MsgBox(msg,vbYesNo)if answer=vbYes thenwshshell.RegDelete mykeyelse increment countercounter=counter+1 updatewshshell.RegWrite mykey,counter,“REG_DWORD”end iffunction ReadKey(key)on error resume nextReadKey=wshshell.RegRead(key)if not err.number=0 thenReadKey=0414Part IV:Accessing the Operating SystemII4684-8 ch14.f.qc 3/3/00 9:39 AM Page 414err.clearend ifend functionThis script uses the Registry as a global data store and maintains a counter.Each time you launch the script,it increments the counter and tells you howoften you have run it.Once you choose to reset,it deletes the counter andstarts over again.Use the key“HKCUSoftware”as the base for your own information keys ifyou want to store them on a per-user basis.Use the key“HKLMSoftware”if you want to store common information.If you changed the script to“HKLMSoftware.”,it would count script usage no matter which user launched the script.In all cases,you need the proper permissions to modify the Registry.Note the changes in ReadKeyif the key doesnt exist,the function returns 0,indicating that there is no counter.Note also that deleting the key doesntremove all of your ent

    注意事项

    本文(Windows脚本编程核心技术精解Chapter14.pdf)为本站会员(asd****56)主动上传,淘文阁 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知淘文阁 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于淘文阁 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

    本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

    工信部备案号:黑ICP备15003705号 © 2020-2023 www.taowenge.com 淘文阁 

    收起
    展开