Collected C # common class code

Source: Internet
Author: User

// -- Method
Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. text;
Using system. Globalization;
Using system. Text. regularexpressions;
Using system. IO;

/// <Summary>
/// Indicates a common method on the web, such as displaying the prompt information, retrieving the date, and logging logs.
/// </Summary>
Public class Webcom
{
Public Webcom ()
{

}
/// <Summary>
/// Display the alert message box of JavaScript, and export the message box
/// </Summary>
/// <Param name = "orgpage"> </param>
/// <Param name = "strmessage"> </param>
Public static void showjsmessage (page orgpage, string strmessage)
{
Random random = new random ();
String strkey = "_ showmessage" + random. Next (10000). tostring () + "()";
If (orgpage. clientscript. isstartupscriptregistered (strkey ))
{
Strkey = "_ showmessage" + random. Next (10000). tostring () + "()";
}
String strscript = "<script language = \" javascript \ "> \ n \ r" +
"Alert (\" "+ strmessage. Replace (" \ r ", convert. tostring (13). Replace (" \ n ", convert. tostring (10 ))

+ "\") \ N \ r "+" </SCRIPT> \ n \ r ";
Type cstype = orgpage. GetType ();
Orgpage. clientscript. registerstartupscript (cstype, strkey, strscript );
}
/// <Summary>
/// Display the Javascript message box and publish the message box
/// </Summary>
/// <Param name = "orgpage"> </param>
/// <Param name = "errormessage"> </param>
/// <Param name = "messheight"> </param>
/// <Param name = "messwidth"> </param>
Public static void showjsmessage (page orgpage, string errormessage, int messheight, int messwidth)
{
Random random = new random ();
String strkey = "_ showerrormessage" + random. Next (10000). tostring () + "()";
If (orgpage. clientscript. isstartupscriptregistered (strkey ))
{
Strkey = "_ showerrormessage" + random. Next (10000). tostring () + "()";
}
String strscript = "<script language = \" javascript \ "> \ n \ r" +
"$ (Document). Ready (showerrormessage); \ n \ r" +
"Function showerrormessage ()" +
"{\ N \ r" +
"Showmessagebox (\" "+ errormessage. Replace (" \ r ", convert. tostring (13). Replace (" \ n ", convert. tostring (10 ))

+ "\", "+ Messwidth +", "+ messheight +") \ n \ r "+"} \ n \ r </SCRIPT> \ n \ r ";
Type cstype = orgpage. GetType ();
Orgpage. clientscript. registerstartupscript (cstype, strkey, strscript );

}
/// <Summary>
/// Give the system prompt
/// </Summary>
/// <Param name = "verrormessage"> </param>
/// <Returns> </returns>
Public static string showsystemtip (string verrormessage)
{
Stringbuilder errormessage = new stringbuilder ();
Errormessage. Length = 0;
Errormessage. append ("<Div style = \" padding: 20px; width: 200px; height: 150px \ "> <Div class = \" X-Box-blue \ "> <Div class = \" X-Box-Tl \ "> <Div class = \" X- box-tr \ "> <Div class = \" X-Box-TC \ "> </div> ");
Errormessage. append ("<Div class = \" X-Box-mL \ "> <Div class = \" X-Box-Mr \ "> <Div class = \" X-Box -MC \ "> ");
Errormessage. append ("Errormessage. append ("</div> ");
Errormessage. append ("<Div class = \" X-Box-BL \ "> <Div class = \" X-Box-Br \ "> ");
Errormessage. append ("<Div class = \" X-Box-BC \ "> </div> ");
Errormessage. append ("</div> ");
Return errormessage. tostring ();
}
/// <Summary>
/// Give the system prompt information and specify the recipient of the information
/// </Summary>
/// <Param name = "vtip"> </param>
/// <Param name = "vheight"> </param>
/// <Param name = "vwidth"> </param>
/// <Returns> </returns>
Public static string showsystemtip (string vtip, int vheight, int vwidth)
{
Stringbuilder errormessage = new stringbuilder ();
Errormessage. Length = 0;
Errormessage. append ("<Div style = 'padding: 20px; '> <Div style = 'width:" + vwidth + "PX; 'class = 'main-Div '> <Div class = 'main-top-left'> <Div class = 'main-top-Right'> <Div class = 'main- top-headers> system prompt information </div> ");
Errormessage. append ("<Div class = 'main-middle-left'> <Div class = 'main-middle-Right'> <Div class = 'main-content-border'> <div class = 'main-content' style = 'height: "+ vheight +" PX; padding: 5px; font-size: 11pt; color: red'> ");
Errormessage. append (vtip );
Errormessage. append ("</div> <Div class = 'main-bottom-left'> <Div class = 'main-bottom- right '> <Div class = 'main-bottom-middle'> </div> ");
Return errormessage. tostring ();
}
/// <Summary>
/// Encrypted characters to prevent leakage of sensitive information.
/// </Summary>
/// <Param name = "S"> </param>
/// <Returns> </returns>
Public static string encodebase (string S)
{
String strresult = "";
If (s! = "" & S! = NULL)
{
Strresult = convert. tobase64string (system. Text. asciiencoding. Default. getbytes (s ));
}
Return strresult;
}

/// <Summary>
/// Decrypt characters to prevent leakage of sensitive information.
/// </Summary>
/// <Param name = "S"> </param>
/// <Returns> </returns>
Public static string decodebase (string S)
{
String strresult = "";
If (s! = "" & S! = NULL)
{
Strresult = system. Text. asciiencoding. Default. getstring (convert. frombase64string (s ));
}
Return strresult;
}

/// <Summary>
/// The root week is the first day of the week.
/// </Summary>
/// <Param name = "weekid"> </param>
/// <Returns> </returns>
Public static string getweekfirstday (string weekid)
{
Int year = int. parse (weekid. substring (0, 4 ));
Datetime newyearday = new datetime (year, 1, 1 );
Int firstweekfirstday = convert. toint32 (newyearday. dayofweek );
Int days = (INT) (7-firstweekfirstday );
Datetime secondweekfirstday = newyearday. adddays (days );
Int week = int. parse (weekid. substring (4, 2 ));
String firstdate = secondweekfirstday. adddays (week-2) * 7). tostring ("yyyy-mm-dd ");
Return firstdate;
}

/// <Summary>
/// The root week is the last day of the week.
/// </Summary>
/// <Param name = "weekid"> </param>
/// <Returns> </returns>
Public static string getweeklastday (string weekid)
{
Int year = int. parse (weekid. substring (0, 4 ));
Datetime newyearday = new datetime (year, 1, 1 );
Int firstweekfirstday = convert. toint32 (newyearday. dayofweek );
Int days = (INT) (7-firstweekfirstday );
Datetime secondweekfisrtday = newyearday. adddays (days );
Int week = int. parse (weekid. substring (4, 2 ));
String lastdate = secondweekfisrtday. adddays (week-2) * 7 + 6). tostring ("yyyy-mm-dd ");
Return lastdate;
}

/// <Summary>
///
/// </Summary>
/// <Param name = "weekid"> </param>
/// <Returns> </returns>
Public static string [] getweekrange (string weekid)
{
String [] day = new string [7];
Int year = int. parse (weekid. substring (0, 4 ));
Datetime newyearday = new datetime (year, 1, 1 );
Int firstweekfirstday = convert. toint32 (newyearday. dayofweek );
Int days = (INT) (7-firstweekfirstday );
Datetime secondweekfisrtday = newyearday. adddays (days );
Int week = int. parse (weekid. substring (4, 2 ));
Datetime firstdate = secondweekfisrtday. adddays (week-2) * 7 );
Datetime lastdate = secondweekfisrtday. adddays (week-2) * 7 + 6 );
Day [0] = firstdate. tostring ("yyyy-mm-dd ");
For (INT I = 1; I <= 5; I ++)
{
Day [I] = firstdate. adddays (I). tostring ("yyyy-mm-dd ");
}
Day [6] = lastdate. tostring ("yyyy-mm-dd ");
Return day;
}

/// <Summary>
/// Returns the week for the root token date
/// </Summary>
/// <Param name = "DT"> </param>
/// <Returns> </returns>
Public static string getweekofyear (datetime DT)
{
Cultureinfo CI = cultureinfo. currentculture;
System. Globalization. Calendar calendar = CI. calendar;
Calendarweekrule rule = CI. datetimeformat. calendarweekrule;
Dayofweek Dow = CI. datetimeformat. firstdayofweek;
Int week = calendar. getweekofyear (DT, rule, Dow );
String weekid = DT. Year + week. tostring ("00 ");
Return weekid;
}
/// <Summary>
/// Determine whether to merge the entire data into the regular table
/// </Summary>
/// <Param name = "Validate"> </param>
/// <Returns> </returns>
Public static bool checkvalidate (string validate)
{
RegEx Re = new RegEx (@ "^ [0-9] + $"); // formula of the positive table

If (! Re. ismatch (validate ))
{
Return false;
}
Else
{
Return true;
}
}
/// <Summary>
/// Producer letter
/// </Summary>
/// <Returns> </returns>
Public static string randomnalpha (int I)
{
String oristr = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstyuwxyz ";
String restr = "";
Random RD = new random ();
For (int K = 0; k <I; k ++)
{
Restr + = oristr [RD. Next (0, oristr. Length)]. tostring ();
}
Return restr;
}
/// <Summary>
/// Inject information into the log file
/// </Summary>
/// <Param name = "textcontent"> log content </param>
/// <Param name = "path"> file path </param>
Public static void writelog (string textcontent, string path)
{
If (! File. exists (PATH ))
{
Using (streamwriter writer = file. createtext (PATH ))
{
Writer. writeline (textcontent + "\ n ");
Writer. Close ();
// String log_source = configurationmanager. receivettings ["Event Log source"];
// System. Diagnostics. EventLog. writeentry (log_source, textcontent, system. Diagnostics. eventlogentrytype. Error );

}
}
Else
{
Using (streamwriter writer2 = file. appendtext (PATH ))
{
Try
{
Writer2.writeline (textcontent + "\ n ");
Writer2.close ();
}
Catch
{

}
}
}
}
/// <Summary>
/// Character Truncation
/// <Para> </para>
/// <Para> C # encoding uses Unicode 16 (ucs2) encoding, which cannot be processed. </para>
///
/// </Summary>
/// <Param name = "oristring"> </param>
/// <Param name = "length"> </param>
/// <Returns> </returns>
Public static string substr (string oristring, int length)
{
Byte [] bytes = system. Text. encoding. Unicode. getbytes (oristring );
Int n = 0;
Int I = 0;
For (; I <bytes. getlength (0) & n <length; I ++)
{
If (I % 2 = 0)
{
N ++;
}
Else
{
If (Bytes [I]> 0)
{
N ++;
}
}
}
If (I % 2 = 1)
{
If (Bytes [I]> 0)

I = I-1;
Else
I = I + 1;
}
Return System. Text. encoding. Unicode. getstring (bytes, 0, I );

}

}

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.