Using GUID to generate the unique file name in C#
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#的更多相关文章
- Creating a Unique File Name
If you are working with files or file attachments in PeopleCode, you will typically want to create a ...
- Unity3d导入工程出现错误“Creating unique file”的解决方法
Unity3d导入工程出现错误“Creating unique file:creating file Temp/tempFile failed.Please ensure there is enoug ...
- Shell: how to list all db links in oracle DB to generate a flat file (生成dblink列表文件)
如果数据库里有上百个DATABASE LINK, 而且同时要管理几十套这样的数据库,在日后改数据库用户密码时就要格外注意是否有DB LINK在使用,否则只改了LOCAL DB 的用户密码,没有级连修改 ...
- executing in nfs will not generate core dump file
最近遇到了一个奇怪的问题. linux系统的pc搭建nfs server,开发板作为nfs client,开发板中全程root权限操作,执行的程序放到 nfs server 中 exports 出的目 ...
- Fedora 24中的日志管理
Introduction Log files are files that contain messages about the system, including the kernel, servi ...
- mybatis反向生成sql,基本的增删改查
用到的几个文件 MyBatisGeneratorProxy.java package com.timestech.wsgk.test.tools; import static org.mybatis. ...
- GUID概念
GUID概念 GUID: 即Globally Unique Identifier(全球唯一标识符) 也称作 UUID(Universally Unique IDentifier) . GUID是 ...
- UUID GUID
http://baike.baidu.com/link?url=xkck9gR5bzOx0oBKP1qNJwGGq3IO56V4i8cg9zTSpSDMVBMA0F7jr0AdkQTGyk7F0FGj ...
- codeforces 710E E. Generate a String(dp)
题目链接: E. Generate a String time limit per test 2 seconds memory limit per test 512 megabytes input s ...
随机推荐
- Tomcat禁止显示目录和文件列表
Tomcat禁止显示目录和文件列表 打开 tomcat的安装目录/conf/web.xml 文件 <servlet> <servlet-name>default</s ...
- 常见的Robotium测试的问题总结:
常见的Robotium测试的问题总结:(从别处拿来的先mark一下) 1.Robotium的 测试类ActivityInstrumentationTestCase2继承了TestCase类,即robo ...
- Microsoft Internet Explorer 远程代码执行漏洞(CVE-2013-3186)(MS13-059)
漏洞版本: Microsoft Internet Explorer 6 - 10 漏洞描述: BUGTRAQ ID: 61663 CVE(CAN) ID: CVE-2013-3186 Windows ...
- 如何给循环中的对象添加事件--深入理解JavaScript的闭包特性
初学者经常碰到的,即获取HTML元素集合,循环给元素添加事件.在事件响应函数中(event handler)获取对应的索引.但每次获取的都是最后一次循环的索引.原因是初学者并未理解JavaScript ...
- HDOJ/HDU 1297 Children’s Queue(推导~大数)
Problem Description There are many students in PHT School. One day, the headmaster whose name is Pig ...
- java常见错误云集与注意事项
1.安装Eclipse时,尽量jdk不要用jre,jdk包含jre j JDK就是Java Development Kit.简单的说JDK是面向开发人员使用的SDK,它提供了Java的开发环境和运行环 ...
- Google生活
本博文的主要内容有 .Google生活的介绍 1.Google生活的介绍 Google生活搜索的官网: http://shenghuo.google.cn/shenghuo/ http://sheng ...
- Validate XML using a XSD (XML Schema)
Consider this XML file howto.xml : <?xml version="1.0" encoding="ISO-8859-1"? ...
- (三)phpcms之文件目录
刚刚接触phpcms,先从它的目录结构说起. 如下图所示,是phpcms的主目录结构: 其中api是接口目录,这个接口不是很明白.大概其是把别的内容加入进来,比如论坛啊什么的. caches是缓存文件 ...
- ASP.NET通过http/https的POST方式,发送和接受XML文件内容
本文转载:http://hi.baidu.com/ysyhyt/item/5011ae39ce3cf49fb80c0395 本文参考:http://blog.csdn.net/ououou123456 ...