The operations on files in youcun-C #

Source: Internet
Author: User
Today's job is to dynamically generate a file. The file generated varies according to user settings. In fact, the file to be generated is a text file, the text content is. aspx. CS Code .
An example has been written for the file, and some values that need to be set by the user are placed as variables in the region to be changed. After the user modifies the value, you only need to change the values of these variables.

Sample Code cut:

Protected System. Web. UI. webcontrols. Button btsearch;
Protected System. Web. UI. webcontrols. textbox tbsearch;
Protected System. Web. UI. webcontrols. dropdownlist community;


// The following areas need to be changed:
// -- Begin --
Protected   String Pageid = " 10 " ;
Protected   String Tablename = " Ljf " ;
Protected Arraylist al = New Arraylist ();

Protected   Void Setarraylist ()
{
Al. Clear ();
Al. Add ("Neighborhood committee name");
Al. Add ("Garbage room location");
}
// -- End --

Protected   Void Checkdelete ( Object Sender, system. eventargs E)
{
(Linkbutton) sender). Attributes. Add ("Onclick","Return isdelete ();");
}

The place to be changed starts with // -- begin and ends with // -- end.

However, it was very troublesome. After several hours of searching for help, I found that in the streamwriter class in C #, The filestream class only supports simple writing and reading of files. To extract the string to be modified in the middle and save it back to the file after modification, I did not do it!

Later, I found a solution, that is, to read all the files and put them in the string object. The string operations in the string class are much better. The solution code is as follows: String Path = @" D: \ browser. aspx. CS " ;
Streamreader SR =   New Streamreader (path, encoding. Default ); // Reading a file into a stream
String Allstr = Sr. readtoend (); // Put the content in the file into the string object allstr
Sr. Close ();

// Next, we split the file, then fill in the corresponding set variables, and finally integrate them.
// Peel off file-Header
Int Begin = Allstr. indexof ( " // -- Begin -- " ) + 11 ;
String Strbegin = Allstr. substring ( 0 , Begin );

// Strip files-tail score
Int End = Allstr. indexof ( " // -- End -- " );
String Strend = Allstr. Remove ( 0 , End );

// Set the variable value in the suffix code
String Strmodify = " Protected string pageid = \ "" + Spageid + " \ " ; \ N " ;
Strmodify = Strmodify + " Protected string tablename = \ "" + Stablename + " \ " ; \ N " ;
Strmodify = Strmodify + " Protected arraylist Al = new arraylist (); \ n " ;
Strmodify = Strmodify + " Protected void setarraylist () \ n " ;
Strmodify = Strmodify + " {Al. Clear (); \ n " ;

String Strmodify2 = "" ;
For ( Int I = 0 ; I < Sserach. Count; I ++ )
{< br> strmodify2 = strmodify2 + " Al. add (\ "" + sserach [I]. tostring () ++ " \ " ); \ n " ;
}

Strmodify = Strmodify + Strmodify2;

Strmodify = Strmodify + " } \ N " ;

// Save the modified file.

Streamwriter SW = File. createtext (sfolder + " \ Browser. aspx. CS " );
Sw. Write (strbegin + Strmodify + Strend ); // Header + modified text + tail = modified code
Sw. Close ();

So far, the tough problem is solved. Happy ~~~

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.