Mike Cat (Mikecat)
From: Old cat の Ide
I introduced the file operation object under System.io space, here I explain how to make a counter applying to a specific page. Each counter requires a corresponding file to store the current number of visits, so how to build a technology file, how to read and write the technical file and how to display the current number of visits is the problem we need to solve. First get a technical file because it is a single page counter, which is not applied to a specific page, so you must obtain or generate different technical files depending on the current page. It is best to use different extensions from the same name as the page. Let's pack the code for getting the counter file part 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 to the current page. The INSTRREV function returns a string from the position MID function that appears from the end in another string to return to the specified number of characters (String, Start [, Length]) parameter string: string expressions from the string: string expressions Returns the character from it. Returns NULL if string contains NULL. START: The start position of the character part of the sum of the string is extracted. If START exceeds the number of characters in String, the MID will return zero length string (""). Length: The number of characters to be returned. If omitted or longens exceeds the text of the text (including the character at Start), use the MID (Mike_Path, 1, Mike_Position) to end from the Start to the string using the MID (Mike_Path, 1, Mike_Position), and intercepted , Then connect our defined extensions to strings, get the count file name we need.
Then read the count file
Below we want to perform the read and write operations for the count file, first we use the previously defined Mike_GetFileName () function to get the current file name:
Dim Mike_Pathname As String
Mike_pathname = Mike_GetFileName ()
Instantiate a read channel for that file, that is, create a FileStream object:
Mike_Stream = New filestream (Mike_Pathname, FileMode.Openorcreate, FileAccess.Read)
Preventing an exception, the page is first accessed, and the count file is created using OpenorCreate.
Let's read the file using the StreamReader object:
Mike_readobj = new streamreader (Mike_Stream)
Mike_str = Mike_Readobj.readline ()
Mike_readobj.close ()
Here we applied to the constructor of the StreamReader object, the constructor of the StreamReader object, and then use the Readline () method to read the file content, you need special attention to
Mike_readobj.close ()
This statement is very important, otherwise the current count file has been exclusively by the StreamReader object, and the file will fail when the file is written, so you must use the close () method to release the files and system resources occupied by the StreamReader object. Secondly show current visits
The current number of visits can be displayed by the response.write () method, the disadvantage of this method is that the display position of the counter cannot be adjusted. Here we use the server control Label in ASP.NET, set the TEXT attribute of the control in the business logic code area. This reflects the advantages of business logic and page logic code separation. However, after all, in a file, I personally recommend the code after the post page, the front page is placed on the ASPX file, and the background file is saved as a class file.
Count = CINT (Mike_Str)
COUNT = 1
Mikecat.text = count
We convert the count files read by streamReader to a value, then count the value plus 1, and then display it in the Label control. Mikecat at this time is the Label control.
Last written counter file
A write channel is still required before writing a write operation:
Mike_Stream = New filestream (Mike_Pathname, FileMode.Open, FileAccess.write)
Since the count file already exists, we only use FileMode.Open
We use the constructor of the StreamWriter object to generate a streamwriter object to perform file write:
Mike_writerobj = new streamwriter (Mike_Stream)
Mike_writerobj.writeline (count)
Mike_writerobj.close ()
Let's give all counter code, you can understand the details
<% @ Page language = "vb" debug = "true" explicit = "true"%>
<% @ Import namespace = "system.io"%>
hEAD>
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>
body>
html>
We will become a beautiful graphic counter here, and the two counters are exactly the same as file reading and writing. Controls for loading pictures in ASP.NET are image controls, so the method of dynamically generating image controls is implemented with dynamically generated graphics. We use the
We need 10 pictures, namely 0 ~ 9 ten numbers (name 0 ~ 9.gif), we use the following Method Dynamic Generating Images: DIM MIKE_IMG As ImageDIM 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 decide the length of the graph that needs to be generated according to the length of the current count string. New instructions to dynamically generate image controls, and then specify the image path to display with the ImageURL property. Finally, use the ADD method of MIKE_DIV to add the currently generated image control to the container collection. Ok, add this code to the counter code above it into a graphic counter. Ha ha. Try everyone! ~