Using GUID to generate the unique file name in C #

Source: Internet
Author: User

GUID, the abbreviation of "Global Unique Identifier", is a unique reference number used as an Identifier in computer softw Is.

GUIDs is usually stored as 128-bit values, and is commonly displayed as + hexadecimal digits with groups separated by H Yphens, such as:

    • 21ec2020-3aea-4069-a2dd-08002b30309d

My new task is to transfer the signature information, which directly saved as bytes[] data in DB. What I ' m gonna to does is to give every signature a unique name and save the file name in DB. The GUID is perfect for my requirement.

Step1:get the img bytes from DB

byte [] DecodedImage = (byte[]) reader["Signature"];

Step2:transfer bytes to img

using New MemoryStream (DecodedImage)) {    = Image.fromstream (ms);}

Step3:give the img a unique name

using New MemoryStream (DecodedImage)) {     = Image.fromstream (ms);       var". png"  ;}

Step4:save the file in System Media folder and Save the file name in DB

using(MemoryStream ms =NewMemoryStream (DecodedImage)) {Image img=Image.fromstream (MS); varImgfilename = Guid.NewGuid (). ToString () +". PNG"; FileName=Imgfilename; Idarr= reader["Id"].    ToString (); stringPath =Path.GetFullPath (Signature_path); img. Save (Path +  imgfilename, System.Drawing.Imaging.ImageFormat.Png);
}

stringsql ="UPDATE [Kiwi-uat]. [dbo]. [Staffclocksignature]"+"SET signimagefile= '"+ FileName +"' WHERE id="+Idarr;using(SqlCommand cmd =NewSqlCommand (SQL, conn)) {    Try{cmd.    ExecuteNonQuery (); }    Catch(System.Exception e) {Console.WriteLine (e.message); }}
    • Tips:using These package at the begining of your file
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient;  using  System.IO; using System.Drawing;

Now I ' m got the PNG file in my folder and the FileName record in DB table ^_^:

Using GUID to generate the unique file name in C #

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.