首页 > Windows开发 > 详细

Using GUID to generate the unique file name in C#

时间:2016-07-19 09:39:04      阅读:202      评论:0      收藏:0      [点我收藏+]

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

GUIDs are usually stored as 128-bit values, and are commonly displayed as 32 hexadecimal digits with groups separated by hyphens, 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 do is to give every signature a unique name and save the file name in DB. GUID is perfect for my requirement.

Step1: Get the img bytes from DB

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

 

Step2: Transfer bytes to img

using (MemoryStream ms = new MemoryStream(decodedImage))
{
    Image img = Image.FromStream(ms);
}

 

Step3: Give the img a unique name

using (MemoryStream ms = new MemoryStream(decodedImage))
{
     Image img = Image.FromStream(ms);
     var imgFileName = Guid.NewGuid().ToString() + ".png";
}

 

Step4: Save the file in system media folder and save the file name in DB

using (MemoryStream ms = new MemoryStream(decodedImage))
{
    Image img = Image.FromStream(ms);
    var imgFileName = Guid.NewGuid().ToString() + ".png";
    fileName = imgFileName;
    idArr = reader["Id"].ToString();
    string path = Path.GetFullPath(SIGNATURE_PATH);
    img.Save(path + imgFileName, System.Drawing.Imaging.ImageFormat.Png);
}

 

string sql = "UPDATE [Kiwi-UAT].[dbo].[StaffClockSignature]" +
                                    "SET SignImageFile=‘" + fileName +
                                    "‘ WHERE Id=" + idArr;
using(SqlCommand cmd = new SqlCommand(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#

原文:http://www.cnblogs.com/ivyfu/p/5683398.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!