Asp. Summary of StreamReader objects in net

Source: Internet
Author: User
Tags constructor control label integer numeric value readline requires servervariables
Asp.net|stream| object in front of me I introduced the file manipulation object in System.IO space, here I explain how to make a counter that applies to a particular page. Each counter requires a corresponding file to store the current amount of traffic, so how to build the file for the technology, how to read and write the technical files, and how to display the current traffic is the problem we need to solve.

Get technical Files First

Because it is a single page counter and is not applied to a particular page, you must be able to obtain or generate different technical files based on the current page. It is a good idea to have a different extension of the same name as the technical file.

We wrap the code that gets the part of the counter file into a function:


function Mike_getfilename () As String
Dim Mike_path As String
Dim mike_position As Integer
Mike_path=request.servervariables ("path_translated")
Mike_position=instrrev (Mike_path, ".")
Mike_getfilename=mid (mike_path,1,mike_position) & "Count"
End Function


Use the ServerVariables ("path_translated") method of the request object to get the absolute path of the current page.
The InStrRev function returns the position of a string that appears from the end in another string
The mid function is used to return a specified number of characters mid (string, start[, length) from a string
Parameters
String: An expression that returns a character from. If string contains null, NULL is returned.
The start position of the character part that is extracted in the start:string. If start exceeds the number of characters in string, Mid returns a 0 length string ("").
Length: The number of characters to return. If omitted or length exceeds the number of characters in the text (including the character at start), all characters in the string ending from start to string are returned

Use Mid (mike_path,1,mike_position) to intercept the file name of the current page and., and then connect our defined extensions to the string, and we get the count file name we need.



Then read the Count file

The next thing we're going to do is read and write the Count file, and first we use the Mike_getfilename () function we defined earlier to get the current file name:

Dim mike_pathname As String
Mike_pathname=mike_getfilename ()


Instantiates a read channel to the file, creating a FileStream object:

Mike_stream=new FileStream (Mike_pathname,filemode.openorcreate,fileaccess.read)


Prevents an exception that is the first time the page is accessed and the count file is created using OpenOrCreate.
The following uses the StreamReader object to read the file:
Mike_readobj=new StreamReader (Mike_stream)
Mike_str=mike_readobj.readline ()
Mike_readobj.close ()

Here we apply the constructor of the StreamReader object described earlier to generate the StreamReader object, and then use the ReadLine () method to read the contents of the file, which requires special attention
Mike_readobj.close ()
This statement is important, otherwise the current count file is always StreamReader object exclusive, the following writes to the file will fail, so you must use the close () method to release the files and system resources that are occupied by the StreamReader object.

Next show current traffic

The current amount of traffic can be displayed using the Response.Write () method, which has the disadvantage of not being able to adjust the display position of the counter. Here we use the server control label in asp.net to set the Text property of the control in the business logic code area. This reflects the business logic and the advantages of the separation of the logical Code of the page, but after all, in a file, I personally recommend the code after the front page is placed in the ASPX file, the background file saved into a class file.


Count=cint (MIKE_STR)
Count+=1
Mikecat.text=count


We convert the Count file read from StreamReader to a numeric value, then add 1 to the value and then display it in the label control. The mikecat at this point is the label control.

Last written to counter file

A write channel is still required before the write operation:
Mike_stream=new FileStream (Mike_pathname,filemode.open,fileaccess.write)
Because the count file already exists at this time, we only use the FileMode.Open
We use the constructor of the StreamWriter object to generate a StreamWriter object for file write operations:

Mike_writerobj=new StreamWriter (Mike_stream)
Mike_writerobj.writeline (count)
Mike_writerobj.close ()




Here are all the counter codes, so you can find out the details.


<%@ Page language= "VB" debug= "true" explicit= "true"%>
<%@ Import namespace= "System.IO"%>

<title> New asp.net Document </title>
<body bgcolor= "#FFFFFF" topmargin= "0" marginheight= "0" >

<script runat= "Server" Language= "VB" >

Sub Page_Load (Source as Object, E as EventArgs)
If not Page.IsPostBack Then
Dim count As Integer
Dim Mike_stream as FileStream
Dim Mike_pathname,mike_str As String
Dim Mike_readobj as StreamReader
Dim Mike_writerobj as StreamWriter
Mike_pathname=mike_getfilename ()
Mike_stream=new FileStream (Mike_pathname,filemode.openorcreate,fileaccess.read)
Mike_readobj=new StreamReader (Mike_stream)
Mike_str=mike_readobj.readline ()
Mike_readobj.close ()
Count=cint (MIKE_STR)
Count+=1
Mikecat.text=count
Mike_stream=new FileStream (Mike_pathname,filemode.open,fileaccess.write)
Mike_writerobj=new StreamWriter (Mike_stream)
Mike_writerobj.writeline (count)
Mike_writerobj.close ()
End If
End Sub

Function Mike_getfilename () As String
Dim Mike_path As String
Dim mike_position As Integer
Mike_path=request.servervariables ("path_translated")
Mike_position=instrrev (Mike_path, ".")
Mike_getfilename=mid (mike_path,1,mike_position) & "Count"
End Function

</script>

<form runat= "Server" method= "" >
<asp:label id= "Mikecat" runat= "Server"/>
</form>

</body>
We have slightly modified here to become a beautiful graphics counter, the two counters in the file read and write exactly the same. The control used to load pictures in asp.net is an image control, so the following method of dynamically generating an image control is used to dynamically generate graphics. This container we use <div id= "Mike_div" runat= "Server"/> Here we need to prepare 10 maps, respectively, 0~9 10 digital pictures (named 0~9.gif), we use the following method to dynamically generate images: Dim mike_ IMG as Image
Dim mike_graph As String
Dim i As Integer
Count=cint (MIKE_STR) +1
For I=1 to Len (count)
Mike_img=new image ()
Mike_graph=mid (count,i,1)
Mike_img.imageurl=mike_graph & ". gif"
Mike_div.controls.add (MIKE_IMG)
Next
First we determine the length of the graph that needs to be generated based on the length of the current count string. The new instruction dynamically generates the image control and then uses the ImageUrl property to specify the path of the picture to display. Finally, use the Mike_div Add method to add the currently generated image control to the container's control collection. OK, put this code into the counter code above to become a graphics counter. Oh. Let's try it! ~


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.