Fundamentally avoid uploading files with the same name [Asp.net]

Source: Internet
Author: User

Recently, I was working on a website to regularly generate pages. One problem occurred is that the file name of the generated page is duplicated. At the beginning, I named it by time, that is, when to generate it. Its name is the time of the moment and accurate to milliseconds. It is usually enough to avoid errors, however, unfortunately, I found that the page generation efficiency is far higher than I thought. I tested that 20 pages can be generated within one millisecond, in this way, the previous page is overwritten, and only the last page is left.

One solution is to identify the duplicate names. If the file name already exists, change the name. However, you must determine the name every time. What's more, with the passage of time, there will inevitably be more and more existing pages. Isn't it very troublesome to traverse the directory every time Chengdu was born? Although there may be good searchesAlgorithmBut I think it is better to solve this problem fundamentally.

In this way, the idea of using GUID is generated. The GUID is a globally unique identifier, that is, a globally unique identifier. GUID is a term used by Microsoft. It creates a unique identifier for an object through a specific algorithm. This identifier can be globally unique.

A guid contains 128 characters in length (16 bytes), such as XXXXXXXX-XXXX-xxxxxxxxxxxx. Each X is a hexadecimal number in the range of 0-9 or a-f. The first four bytes are random numbers, and the 5-8 bytes are timestamps, the GUID generation time (in minutes) is recorded, ensuring that the time is unique; 9-10 bytes are related to the number of machine restarts; the last 6 bytes are generally the NIC address of the machine (if the machine does not have a nic, another constant is used), thus ensuring the uniqueness of the space. Therefore, it is almost impossible to generate two identical guids, thus ensuring the uniqueness of names.

GUID is widely used in. net. At the same time, the. NET Framework provides a specialized guid class, which can be easily generated by users. For example, in Asp.net, you can create a guid: system. guid. newguid (). Based on this principle, I can use this class to name my generated files, fundamentally solving the problem of duplicate names. This method can also be applied to the name of the uploaded file.

For more information, we found that GUID is widely used. In fact, we often see it. Such as the registry, class and interface identification, database, and automatically generated machine name and directory name. You can open the \ windows \ Installer directory on your system disk. You can see many folders similar to {0efdf2f9-836d-4eb7-a32d-038bd3f1fb2a}. This is the directory name automatically generated by the system. You can also find such a number string in the registry. You don't know what it was before. Now you understand that the guid is generated by the system to avoid duplicate names.

More detailed reference: http://blog.mm-share.cn/post/139.html

Cat's garden

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.