[Vbs] writes an INI file to vbs.

Source: Internet
Author: User

In the past two days, I tried to install an installation package for a project. The first time I came into contact with the software package, I used the advanced Installer (AI ), it should be said that if there are no special actions during the installation process (the general actions refer to releasing files, writing the registry, and creating shortcuts), it would be a dumb to create a package according to the wizard and interface operations. However, my installation actions include:

-A ComboBox is provided to List Installed printers for users to select

-Ask the user to fill in some initial parameters and write these parameters to the specified INI and XML files in the installation directory after installation.

The first requirement left me around for a long time. The difficulty was to dynamically fill in the ComboBox item. The materials and documents were hard to find and e-paper. Fortunately, Tiandao was rewarded with diligence, the answer is to ask me to find useful posts in the official forum, or I will talk about how to do it in another article to help later users.

The next step is to get the user input and write it to the specified INI and XML. I personally think that there are few ways for AI to play freely. Its custom actions (Custom Actions) can call EXE, JS, vbs, and other things, but JS and vbs have nothing to do with them, A little timid. To solve the problem in a familiar way, you can use C # To write an EXE, PASS Parameters through the main function, and write parameters to ini and XML ~ It's easy. But think about it ,. net program depends on CLR, although the installation process can specify the necessary components, force the user to put. net installation, do not worry about running to this.. Net program, but always feel embedded in the installation package. net program seems not authentic. net program needs to wake up the CLR for the first time. What I need to do during the installation process is smooth. For a simple task, I have to wait for the CLR to start. Even if it is only one or two seconds, it cannot be tolerated. Second, even if I have the confidence to learn another non-hosted language (Delphi/C ++) in a short period of time to complete this simple task, the size of the EXE will inevitably exceed the size of the script, this will increase the volume of the entire installation package, not necessary. We decided to engage in vbs.

It's just his mother, who is used to the elegant C #. It feels like other languages, let alone there is no decent ise for scripts like vbs, in short, I admire coding programmers in these languages, who are completely abused. Whatever ~ In the beginning, I was looking for a method for vbs to call Win32 API. If you can, call writeprivateprofilestring and I will find it, it seems that vbs cannot directly call the API (no official statement is found, so there are temporary questions). There are two ways to improve people on the Internet: one is to use vbs to call the office VBA Component, then, use VBA to call the API ~ It is really a detour, but it also lamented the wisdom of high people; second, throwing a dynwrap into the system. the dll can be called. The source of the DLL is still quite mysterious, and no clue is found in the file information and content. The author is actually Lei Feng. Anyway, the above method cannot be considered for installation packages. Therefore, we should study the authentic vbs method honestly. The following is a little achievement:

'Function: Set the INI value (INI path, target node, target key, and target value) 'Note: If the INI file does not exist, it is created. If the node or key does not exist, add function setinivalue (path, sectionname, keyname, value) dim FSO, fileset FSO = Createobject ("scripting. fileSystemObject ") set file = FSO. opentextfile (path, 1, true) dim line, cache, insection, sectionexist, keyexistdo until file. atendofstreamline = file. readlineif strcomp (TRIM (line), "[" + sectionname + "]", 1) = 0 then insection = true sectionexist = trueend ifif 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 ifend ifif 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 ifend ifcache = cache + LINE + vbcrlfloopfile. closeif not sectionexist then cache = cache + "[" + sectionname + "]" + vbcrlf cache = cache + keyname + "=" + value + vbcrlfelseif not keyexist then cache = cache + keyname + "=" + value + vbcrlfend ifset file = FSO. opentextfile (path, 2, true) file. write (cache) file. closeend function 'call setinivalue "C: \ test. ini "," other "," CCC "," AAA"

The principle is to use the text processing method to process INI, read data row by row, and store data directly into the cache when there are unrelated rows. Then, the related rows are processed and then saved to the cache, the final cache is a complete new ini content, which directly writes the cache to the original file and implements "modification ".

-Wen Bi-

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.