C # example of using DLL and API to read and write and delete ini. txt files

Source: Internet
Author: User

Using system; <br/> using system. io; <br/> using system. text; <br/> using system. runtime. interopservices; </P> <p> class inifilehandler <br/> {<br/> [dllimport ("kernel32.dll")] <br/> Public static extern uint <br/> getprivateprofilestring (string lpappname, <br/> string lpkeyname, string lpdefault, <br/> stringbuilder lpreturnedstring, uint nsize, <br/> string lpfilename); </P> <p> [dllimport ("kernel32.dll", entrypoint = "getprivateprofilestringa")] <br/> Public static extern uint <br/> getprivateprofilestringbytearray (string lpappname, <br/> string lpkeyname, string lpdefault, <br/> byte [] lpreturnedstring, uint nsize, <br/> string lpfilename); </P> <p> [dllimport ("kernel32.dll")] <br/> Public static extern uint <br/> getprivateprofileint (string lpappname, <br/> string lpkeyname, int ndefault, string lpfilename); </P> <p> [dllimport ("kernel32.dll")] <br/> Public static extern uint writeprivateprofilestring (<br/> string lpappname, <br/> string lpkeyname, <br/> string lpstring, <br/> string lpfilename ); <br/>}</P> <p> namespace consoleapplication24 <br/>{< br/> class Program <br/>{< br/> static void main (string [] ARGs) <br/>{</P> <p> // Tianjia key Zhi <br/> inifilehandler. writeprivateprofilestring ("foru", "radfactory", "on", @ "C:/setup.txt"); </P> <p> inifilehandler. writeprivateprofilestring ("def", "A1", "on", @ "C:/setup.txt"); <br/> inifilehandler. writeprivateprofilestring ("def", "A2", "0", @ "C:/setup.txt "); </P> <p> // Qu De zifuzhuan <br/> stringbuilder sb = new stringbuilder (1024); <br/> inifilehandler. getprivateprofilestring ("def", "A2", "default", Sb, (uint) Sb. capacity, @ "C:/setup.txt"); <br/> console. writeline ("1 failed: {0}", sb. tostring (); </P> <p> // Qu De int <br/> uint resultvalue = inifilehandler. getprivateprofileint ("appli", "key2", 0, @ "C:/setup.txt"); <br/> console. writeline ("interval 2 failed: {0}", resultvalue ); </P> <p> // Zhi Ding section Qu De <br/> byte [] AR1 = new byte [1024]; <br/> uint resultsize1 = inifilehandler. getprivateprofilestringbybytearray ("Factory", null, "default", AR1, (uint) ar1.length, @ "C:/setup.txt"); <br/> string result1 = system. text. encoding. default. getstring (AR1, 0, (INT) resultsize1-1); <br/> string [] keys = result1.split ('/0 '); <br/> foreach (string key in keys) <br/>{< br/> console. writeline ("Please wait until 1 minute before please wait until your desired region contains your desired region name: {0}", key ); <br/>}</P> <p> // specify the primary new byte [2, 1024]; <br/> uint resultsize2 = inifilehandler. getprivateprofilestringbybytearray (null, null, "default", AR2, (uint) ar2.length, @ "C:/setup.txt"); <br/> string result2 = system. text. encoding. default. getstring (AR2, 0, (INT) resultsize2-1); <br/> string [] sections = result2.split ('/0 '); <br/> foreach (string section in sections) <br/>{< br/> console. writeline ); <br/>}</P> <p> // 1 つのキ <br/> inifilehandler. writeprivateprofilestring ("invalid parameter 2", "invalid parameter 1", null, @ "C:/setup.txt "); </P> <p> // specifies the number of workers in the worker. writeprivateprofilestring ("commandid upload 1", null, null, @ "C:/setup.txt"); </P> <p> console. readline (); </P> <p >}< br/>}

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.