几个C#编程的小技巧 (二) (判断文件或文件夹是否存在 )
一、判断文件或文件夹是否存在
使用System.IO.File,要检查一个文件是否存在非常简单:
bool exist = System.IO.File.Exists(fileName);
如果需要判断目录(文件夹)是否存在,可以使用System.IO.Directory:
bool exist = System.IO.Directory.Exists(folderName);
使用System.IO.File,要检查一个文件是否存在非常简单:
bool exist = System.IO.File.Exists(fileName);
如果需要判断目录(文件夹)是否存在,可以使用System.IO.Directory:
bool exist = System.IO.Directory.Exists(folderName);
如何在C#中实现图片缩放
//************************************************************//
//下面给出三个简单的方法,后面两个方法是扩展,估计有时用得着
//************************************************************//
/// <summary>
/// 缩小图片
/// </summary>
/// <param name="strOldPic">源图文件名(包括路径)</param>
/// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>
/// <param name="intWidth">缩小至宽度</param>
/// <param name="intHeight">缩小至高度</param>
public void SmallPic(string strOldPic, string strNewPic, int intWidth, int intHeight)
{
System.Drawing.Bitmap objPic,objNewPic;
try
{
objPic = new System.Drawing.Bitmap(strOldPic);
objNewPic=new System.Drawing.Bitmap(objPic,intWidth,intHeight);
objNewPic.Save(strNewPic);
}
catch(Exception exp){throw exp;}
finally
{
objPic=null;
objNewPic=null;
}
}
/// <summary>
/// 按比例缩小图片,自动计算高度
/// </summary>
/// <param name="strOldPic">源图文件名(包括路径)</param>
/// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>
/// <param name="intWidth">缩小至宽度</param>
public void SmallPic(string strOldPic, string strNewPic, int intWidth)
{
System.Drawing.Bitmap objPic,objNewPic;
try
{
objPic = new System.Drawing.Bitmap(strOldPic);
int intHeight=(intWidth / objPic.Width) * objPic.Height;
objNewPic=new System.Drawing.Bitmap(objPic,intWidth,intHeight);
objNewPic.Save(strNewPic);
}
catch(Exception exp){throw exp;}
finally
{
objPic=null;
objNewPic=null;
}
}
/// <summary>
/// 按比例缩小图片,自动计算宽度
/// </summary>
/// <param name="strOldPic">源图文件名(包括路径)</param>
/// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>
/// <param name="intHeight">缩小至高度</param>
public void SmallPic(string strOldPic, string strNewPic, int intHeight)
{
System.Drawing.Bitmap objPic,objNewPic;
try
{
objPic = new System.Drawing.Bitmap(strOldPic);
int intWidth=(intHeight / objPic.Height) * objPic.Width;
objNewPic=new System.Drawing.Bitmap(objPic,intWidth,intHeight);
objNewPic.Save(strNewPic);
}
catch(Exception exp){throw exp;}
finally
{
objPic=null;
objNewPic=null;
}
}
//下面给出三个简单的方法,后面两个方法是扩展,估计有时用得着
//************************************************************//
/// <summary>
/// 缩小图片
/// </summary>
/// <param name="strOldPic">源图文件名(包括路径)</param>
/// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>
/// <param name="intWidth">缩小至宽度</param>
/// <param name="intHeight">缩小至高度</param>
public void SmallPic(string strOldPic, string strNewPic, int intWidth, int intHeight)
{
System.Drawing.Bitmap objPic,objNewPic;
try
{
objPic = new System.Drawing.Bitmap(strOldPic);
objNewPic=new System.Drawing.Bitmap(objPic,intWidth,intHeight);
objNewPic.Save(strNewPic);
}
catch(Exception exp){throw exp;}
finally
{
objPic=null;
objNewPic=null;
}
}
/// <summary>
/// 按比例缩小图片,自动计算高度
/// </summary>
/// <param name="strOldPic">源图文件名(包括路径)</param>
/// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>
/// <param name="intWidth">缩小至宽度</param>
public void SmallPic(string strOldPic, string strNewPic, int intWidth)
{
System.Drawing.Bitmap objPic,objNewPic;
try
{
objPic = new System.Drawing.Bitmap(strOldPic);
int intHeight=(intWidth / objPic.Width) * objPic.Height;
objNewPic=new System.Drawing.Bitmap(objPic,intWidth,intHeight);
objNewPic.Save(strNewPic);
}
catch(Exception exp){throw exp;}
finally
{
objPic=null;
objNewPic=null;
}
}
/// <summary>
/// 按比例缩小图片,自动计算宽度
/// </summary>
/// <param name="strOldPic">源图文件名(包括路径)</param>
/// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>
/// <param name="intHeight">缩小至高度</param>
public void SmallPic(string strOldPic, string strNewPic, int intHeight)
{
System.Drawing.Bitmap objPic,objNewPic;
try
{
objPic = new System.Drawing.Bitmap(strOldPic);
int intWidth=(intHeight / objPic.Height) * objPic.Width;
objNewPic=new System.Drawing.Bitmap(objPic,intWidth,intHeight);
objNewPic.Save(strNewPic);
}
catch(Exception exp){throw exp;}
finally
{
objPic=null;
objNewPic=null;
}
}
(C#)数值型货币的大写转换
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WinMarket
... {
/**//// <summary>
/// Classfun 的摘要说明。
/// </summary>
public class Classfun
...{
private string Capstr;
public Classfun()
...{
//
// TODO: 在此处添加构造函数逻辑
//
}
public string MoneyNumtoCap(decimal Num)
...{
string Cap="零壹贰叁肆伍陆柒捌玖";
string Numstr="0123456789";
string MoneyNumstr=Num.ToString();
int Pint=MoneyNumstr.IndexOf(".");
int Numint;
string Moneyint=null;
string Moneydec=null;
string Intstr=null;
string MoneyCap=null;
string Moneyintstr=null;
string Moneydecstr=null;
// Capstr=Pint.ToString();
if(Pint!=-1)
...{
string strArr=".";
char[] charArr=strArr.ToCharArray();
string[] MoneyNumArr=MoneyNumstr.Split(charArr);
Moneyint=MoneyNumArr[0].ToString();
Moneydec=MoneyNumArr[1].ToString();
}
else
...{
Moneyint=MoneyNumstr;
Moneydec="00";
}
if(Moneyint.Length>16)
...{
MessageBox.Show("数值超界");
}
else
...{
//--- 处理整数部分--------
for(int j=1;j<=Moneyint.Length;j++)
...{
Moneyintstr=Moneyint.Substring(j-1,1);
for(int i=0;i<=9;i++)
...{
Intstr=Numstr.Substring(i,1);
MoneyCap=Cap.Substring(i,1);
if(Moneyintstr==Intstr)
...{
switch (Intstr)
...{
case "0":
Capstr=Capstr+MoneyCap;
break;
case "1":
Capstr=Capstr+MoneyCap;
break;
case "2":
Capstr=Capstr+MoneyCap;
break;
case "3":
Capstr=Capstr+MoneyCap;
break;
case "4":
Capstr=Capstr+MoneyCap;
break;
case "5":
Capstr=Capstr+MoneyCap;
break;
case "6":
Capstr=Capstr+MoneyCap;
break;
case "7":
Capstr=Capstr+MoneyCap;
break;
case "8":
Capstr=Capstr+MoneyCap;
break;
case "9":
Capstr=Capstr+MoneyCap;
break;
}
}
}
Numint=Moneyint.Length-j+1;
switch (Numint)
...{
case 16:
Capstr=Capstr+"仟万";
break;
case 15:
Capstr=Capstr+"佰万";
break;
case 14:
Capstr=Capstr+"拾万";
break;
case 13:
Capstr=Capstr+"万";
break;
case 12:
Capstr=Capstr+"仟";
break;
case 11:
Capstr=Capstr+"佰";
break;
case 10:
Capstr=Capstr+"拾";
break;
case 9:
Capstr=Capstr+"亿";
break;
case 8:
Capstr=Capstr+"仟";
break;
case 7:
Capstr=Capstr+"佰";
break;
case 6:
Capstr=Capstr+"拾";
break;
case 5:
Capstr=Capstr+"万";
break;
case 4:
Capstr=Capstr+"仟";
break;
case 3:
Capstr=Capstr+"佰";
break;
case 2:
Capstr=Capstr+"拾";
break;
case 1:
Capstr=Capstr+"元";
break;
}
}
//------处理小数部分-----
for(int j=1; j<=2; j++)
...{
Moneydecstr=Moneydec.Substring(j-1,1);
for(int i=0;i<=9;i++)
...{
Intstr=Numstr.Substring(i,1);
MoneyCap=Cap.Substring(i,1);
if(Moneydecstr==Intstr)
...{
switch (Intstr)
...{
case "0":
Capstr=Capstr+MoneyCap;
break;
case "1":
Capstr=Capstr+MoneyCap;
break;
case "2":
Capstr=Capstr+MoneyCap;
break;
case "3":
Capstr=Capstr+MoneyCap;
break;
case "4":
Capstr=Capstr+MoneyCap;
break;
case "5":
Capstr=Capstr+MoneyCap;
break;
case "6":
Capstr=Capstr+MoneyCap;
break;
case "7":
Capstr=Capstr+MoneyCap;
break;
case "8":
Capstr=Capstr+MoneyCap;
break;
case "9":
Capstr=Capstr+MoneyCap;
break;
}
}
}
switch(j)
...{
case 1:
Capstr=Capstr+"角";
break;
case 2:
Capstr=Capstr+"分";
break;
}
}
}
return Capstr;
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WinMarket
... {
/**//// <summary>
/// Classfun 的摘要说明。
/// </summary>
public class Classfun
...{
private string Capstr;
public Classfun()
...{
//
// TODO: 在此处添加构造函数逻辑
//
}
public string MoneyNumtoCap(decimal Num)
...{
string Cap="零壹贰叁肆伍陆柒捌玖";
string Numstr="0123456789";
string MoneyNumstr=Num.ToString();
int Pint=MoneyNumstr.IndexOf(".");
int Numint;
string Moneyint=null;
string Moneydec=null;
string Intstr=null;
string MoneyCap=null;
string Moneyintstr=null;
string Moneydecstr=null;
// Capstr=Pint.ToString();
if(Pint!=-1)
...{
string strArr=".";
char[] charArr=strArr.ToCharArray();
string[] MoneyNumArr=MoneyNumstr.Split(charArr);
Moneyint=MoneyNumArr[0].ToString();
Moneydec=MoneyNumArr[1].ToString();
}
else
...{
Moneyint=MoneyNumstr;
Moneydec="00";
}
if(Moneyint.Length>16)
...{
MessageBox.Show("数值超界");
}
else
...{
//--- 处理整数部分--------
for(int j=1;j<=Moneyint.Length;j++)
...{
Moneyintstr=Moneyint.Substring(j-1,1);
for(int i=0;i<=9;i++)
...{
Intstr=Numstr.Substring(i,1);
MoneyCap=Cap.Substring(i,1);
if(Moneyintstr==Intstr)
...{
switch (Intstr)
...{
case "0":
Capstr=Capstr+MoneyCap;
break;
case "1":
Capstr=Capstr+MoneyCap;
break;
case "2":
Capstr=Capstr+MoneyCap;
break;
case "3":
Capstr=Capstr+MoneyCap;
break;
case "4":
Capstr=Capstr+MoneyCap;
break;
case "5":
Capstr=Capstr+MoneyCap;
break;
case "6":
Capstr=Capstr+MoneyCap;
break;
case "7":
Capstr=Capstr+MoneyCap;
break;
case "8":
Capstr=Capstr+MoneyCap;
break;
case "9":
Capstr=Capstr+MoneyCap;
break;
}
}
}
Numint=Moneyint.Length-j+1;
switch (Numint)
...{
case 16:
Capstr=Capstr+"仟万";
break;
case 15:
Capstr=Capstr+"佰万";
break;
case 14:
Capstr=Capstr+"拾万";
break;
case 13:
Capstr=Capstr+"万";
break;
case 12:
Capstr=Capstr+"仟";
break;
case 11:
Capstr=Capstr+"佰";
break;
case 10:
Capstr=Capstr+"拾";
break;
case 9:
Capstr=Capstr+"亿";
break;
case 8:
Capstr=Capstr+"仟";
break;
case 7:
Capstr=Capstr+"佰";
break;
case 6:
Capstr=Capstr+"拾";
break;
case 5:
Capstr=Capstr+"万";
break;
case 4:
Capstr=Capstr+"仟";
break;
case 3:
Capstr=Capstr+"佰";
break;
case 2:
Capstr=Capstr+"拾";
break;
case 1:
Capstr=Capstr+"元";
break;
}
}
//------处理小数部分-----
for(int j=1; j<=2; j++)
...{
Moneydecstr=Moneydec.Substring(j-1,1);
for(int i=0;i<=9;i++)
...{
Intstr=Numstr.Substring(i,1);
MoneyCap=Cap.Substring(i,1);
if(Moneydecstr==Intstr)
...{
switch (Intstr)
...{
case "0":
Capstr=Capstr+MoneyCap;
break;
case "1":
Capstr=Capstr+MoneyCap;
break;
case "2":
Capstr=Capstr+MoneyCap;
break;
case "3":
Capstr=Capstr+MoneyCap;
break;
case "4":
Capstr=Capstr+MoneyCap;
break;
case "5":
Capstr=Capstr+MoneyCap;
break;
case "6":
Capstr=Capstr+MoneyCap;
break;
case "7":
Capstr=Capstr+MoneyCap;
break;
case "8":
Capstr=Capstr+MoneyCap;
break;
case "9":
Capstr=Capstr+MoneyCap;
break;
}
}
}
switch(j)
...{
case 1:
Capstr=Capstr+"角";
break;
case 2:
Capstr=Capstr+"分";
break;
}
}
}
return Capstr;
}