C # create a file in winform, write the value to the file, read the value in the file, modify the file value, create, write, and modify the file

Source: Internet
Author: User
  1. # Region: determines whether a file exists. If the file does not exist, it is created. Otherwise, the read value is displayed in the form.
  2. PublicFormmain ()
  3. {
  4. Initializecomponent ();
  5. // Readfile (application. startuppath + "// alarmset.txt ");
  6. // Determine whether a file exists
  7. // System. Io. directoryinfo info = new system. Io. directoryinfo (application. startuppath + "// alarmset.txt ");
  8. // MessageBox. Show (info. exists. tostring ());
  9. // MessageBox. Show (application. startuppath + "// alarmset.txt ");
  10. // Determine whether a file exists
  11. If(! File. exists (application. startuppath + "// alarmset.txt "))
  12. {
  13. // File. Create (application. startuppath + "// alarmset.txt"); // create the file
  14. Filestream fs1 =NewFilestream (application. startuppath + "// alarmset.txt", filemode. Create, fileaccess. Write); // create a Write File
  15. Streamwriter Sw =NewStreamwriter (fs1 );
  16. Sw. writeline ("[runtype]"); // start to write the value
  17. Sw. writeline ("type = 1 ");
  18. Sw. writeline ("/R/N ");
  19. Sw. writeline ("-- alarm setting ppws number board matching bits ppwz matching position 0 First Matching 1 then matching ");
  20. Sw. writeline ("[alarm]");
  21. Sw. writeline ("ppwz = 0 ");
  22. Sw. writeline ("ppws = 8 ");
  23. Sw. writeline ("/R/N ");
  24. Sw. writeline ("[server]");
  25. Sw. writeline ("listenport = 2005 ");
  26. Sw. writeline ("/R/N ");
  27. Sw. writeline ("[Form]");
  28. Sw. writeline ("ppwz = 0 ");
  29. Sw. Close ();
  30. Fs1.close ();
  31. }
  32. // Read the file value and display it to the form
  33. Filestream FS =NewFilestream (application. startuppath + "// alarmset.txt", filemode. Open, fileaccess. readwrite );
  34. Streamreader sr =NewStreamreader (FS );
  35. StringLine = Sr. Readline ();
  36. IntCurline = 0;
  37. While(Line! =Null)
  38. {
  39. If(++ Curline = 7 & line. Equals ("ppwz = 0") // if the value of Row 3 of the file is ppwz = 0, set the match before selecting the single-choice button.
  40. {
  41. Radiobutton1.checked =True;
  42. Radiobutton2.checked =False;
  43. // MessageBox. Show ("before ");
  44. }
  45. Else If(Curline = 8 & line. Equals ("ppwz = 1") // when the value of row 8th is ppwz = 1, set the radio button to match
  46. {
  47. Radiobutton2.checked =True;
  48. Radiobutton1.checked =False;
  49. // MessageBox. Show (" ");
  50. }
  51. If(Curline = 8) // file 8th rows
  52. {
  53. Textbox1.text = line. substring (line. lastindexof ("=") + 1); // capture the value after =
  54. }
  55. // MessageBox. Show ("th" + (++ curline). tostring () + "row:" + line );
  56. // Console. writeline ("th" + (++ curline). tostring () + "row:" + line );
  57. Line = Sr. Readline ();
  58. }
  59. Sr. Close ();
  60. FS. Close ();
  61. }
  62.  
  63. # Endregion

Modify the file value

  1. # Region: Click Save settings.
  2. Private VoidButton6_click (ObjectSender, eventargs E)
  3. {
  4. If(Radiobutton1.checked =True)
  5. {
  6. Editfile (7, "ppwz = 0", application. startuppath + "// alarmset.txt ");
  7. Editfile (8, "ppws =" + textbox1.text, application. startuppath + "// alarmset.txt ");
  8. }
  9. If(Radiobutton2.checked =True)
  10. {
  11. Editfile (7, "ppwz = 1", application. startuppath + "// alarmset.txt ");
  12. Editfile (8, "ppws =" + textbox1.text, application. startuppath + "// alarmset.txt ");
  13. }
  14. }
  15.  
  16. # Endregion
  17.  
  18.  
  19. # Match region settings
  20. Public Static VoidEditfile (IntCurline,StringNewlinevalue,StringPatch)
  21. {
  22. Filestream FS =NewFilestream (patch, filemode. Open, fileaccess. Read );
  23. Streamreader sr =NewStreamreader (FS, encoding. getencoding ("UTF-8"); // solves the garbled characters in the written files.
  24. StringLine = Sr. Readline ();
  25. Stringbuilder sb =NewStringbuilder ();
  26. For(IntI = 1; line! =Null; I ++)
  27. {
  28. SB. append (LINE + "/R/N ");
  29. If(I! = Curline-1)
  30. Line = Sr. Readline ();
  31. Else
  32. {
  33. Sr. Readline ();
  34. Line = newlinevalue;
  35. }
  36. }
  37. Sr. Close ();
  38. FS. Close ();
  39. Filestream fs1 =NewFilestream (patch, filemode. Open, fileaccess. Write );
  40. Streamwriter Sw =NewStreamwriter (fs1 );
  41. Sw. Write (sb. tostring ());
  42. Sw. Close ();
  43. FS. Close ();
  44. }
  45.  
  46. # Endregion
Related Article

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.