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 ...
随机推荐
- IEEE 802
IEEE 802又称为LMSC(LAN /MAN Standards Committee, 局域网/城域网标准委员会), 致力于研究局域网和城域网的物理层和MAC层中定义的服务和协议, 对应OSI网络 ...
- Linux Shell编程(29)——函数
和"真正的"编程语言一样, Bash也有函数,虽然在某些实现方面稍有些限制. 一个函数是一个子程序,用于实现一串操作的代码块,它是完成特定任务的"黑盒子". 当 ...
- Windows下父进程监视子进程状态
最近研究自动化测试,需要获取程序的运行状态及结果,下面是些参考资料. 原文地址:http://blog.csdn.net/ariesjzj/article/details/7226443 Linux下 ...
- Milk Patterns - poj 3261 (求重复k次的最长子串)
题目大意:给你一个数组,求这个数组里面至少重复k次的子串. 分析:后缀数组的练手题目...不过给的数字比较大,可以先离散化处理一下即可. 代码如下: ===================== ...
- poj 2631 Roads in the North【树的直径裸题】
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2359 Accepted: 115 ...
- 53个要点提高php效率
用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数”(译注:PHP手册中说 ...
- C# 字符串常用操作 分类: C# 2014-08-22 15:07 238人阅读 评论(0) 收藏
string str1 = "C#操作字符串<几种常见方式>如下"; string str2 = "C#操作字符串"; //比较字符串 Co ...
- java中的上转型对象
1. 定义 如果B类是A类的子类或间接子类,当用B类创建对象b并将这个对象b的引用赋给A类对象a时,如: A a;a = new B();ORA a;B b = new B();a = b; 通俗的说 ...
- vim note(6)--vim的一个较全的介绍(转)
vim的配置文件 ~/.vimrc 用户的默认配置文件 ~/.vim/plugin/ 用户的默认脚本文件的存放文件夹 ~/.vim/ftplugin/ 用户的默认文件类型相关脚本文件的 ...
- 韩玉琪 《Linux内核分析》MOOC课程
http://www.cnblogs.com/hyq20135317/p/5422516.html http://mooc.study.163.com/course/USTC-1000029000