[vbs] vbs write ini file
These two days in the toss to a project installation package, the first contact with software packaging, using the Advanced Installer (hereinafter referred to as AI), I should say that if the installation process does not have special action (regular action refers to release files, write registry, build shortcuts, etc.), but quite foolish, follow the wizard and interface operation can be a good package. But my installation actions include.
- Provides a ComboBox listing of installed printers for the user to select
- Have the user fill in some initial parameters and write them to the specified ini and xml files in the installation directory after the installation is complete
The first requirement took me most of the day, the difficulty is to dynamically fill ComboBox items, information and documentation is not easy to find, and the E text, but in the good fortune, I was able to find useful posts in the official forum, finally get it done, or I will open a separate article on how to do it, to help the later.
The next step is to get the user's input， Then write the specifiedini harmonyxml in。AI There are not too few avenues for free play personally， It's self-setting action（Custom Actions） may invokeexe/js/vbs et cetera， howeverjs harmonyvbs I don't have much of a background.， It's a little intimidating.， Trying to solve the problem in a way that you are familiar with， that is, withc# Write aexe， pass (a bill or inspection)main function pass on references， Write the parameters to theini harmonyxml~ It's easy.。 But think about it.， on one hand,...，.net Program dependenciesCLR， Although the installation process can specify the necessary components， Forcing users to put.net fit up， Don't worry about executing to that.net Lack of operating environment at the time of the program， But it always feels like embedding in the installer.net The procedure doesn't seem quite right.， there is more.net Wakeup required for first run of programCLR， My installation process is all about smoothness， Waiting for a simple task.CLR launch， Even if it's just for a second or two.， And something I can't stand.； secondly， Even if I were confident that I could learn another non-hosted language in a short time（delphi/c++ What's that?） to complete this simple task， howeverexe The volume is also bound to be considerably larger than the scripted， This will increase the size of the entire installation package， unnecessary。 All the thinking that went into it， Still decided to have a govbs。
It's just that heck, I'm so used to elegant C# that I feel like shit with other languages, not to mention that there's nothing decent ISE for scripts like vbs, and all in all I admire the coders who mainly coding on these languages, it's totally masochistic. Whatever~ At first, I wanted to find the way to call the Win32 API with vbs, if I could, then I would adjust WritePrivateProfileString and be done with it, but I searched and searched and found that vbs can't directly adjust the API (I didn't find the official statement, so I have doubts for the time being), there are two ways of high people on the Internet: one is to call the vba component of office with vbs, and then operate vba to call the api ~ really a roundabout way, and also sigh the wisdom of high people na; two is to throw a dynwrap.dll to the system can be adjusted, said the origin of this dll is still quite mysterious, can't search, file information and content also didn't see any clues, the human author of this is the real Thunderbolt ah. Anyway, the above method can never be considered for me to do the installer, so it's better to honestly research the authentic vbs method with small results and share the following.
' function： set upini happen to（ini trails， target node， target key， target value） ' note： ifini If the file does not exist, create； Add if node or key does not exist Function SetIniValue(path, sectionName, keyName, value) Dim fso,file Set fso = CreateObject("Scripting.FileSystemObject") Set file = fso.OpenTextFile(path, 1,true) Dim line, cache, inSection, sectionExist, keyExist Do Until file.AtEndOfStream line = file.Readline if StrComp(Trim(line),"["+sectionName+"]",1)=0 Then inSection=True sectionExist=True End If if inSection And Left(LTrim(line),1)="[" And StrComp(Trim(line),"["+sectionName+"]",1)<>0 Then inSection=False If Not keyExist Then cache = cache + keyName+"="+value+vbCrLf keyExist=True End If End If if inSection And InStr(line,"=")<>0 Then ss = Split(line,"=") If StrComp(Trim(ss(0)),keyName,1)=0 Then line = ss(0)+"="+value keyExist = True End If End If cache=cache+line+vbcrlf Loop file.Close If not sectionExist Then cache = cache + "["+sectionName+"]"+vbCrLf cache = cache + keyName+"="+value+vbCrLf ElseIf Not keyExist Then cache = cache + keyName+"="+value+vbCrLf End If Set file = fso.OpenTextFile(path, 2, True) file.Write(cache) file.Close End Function ' invoke SetIniValue "C: est.ini","Other","ccc","aaa"
The principle is to use the way of processing text to process the ini, read it line by line, and deposit it directly into the cache when it encounters irrelevant lines, and then deposit it into the cache after the relevant lines are processed accordingly, and finally the cache is a complete copy of the new ini's content.
- Man Bi -