⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cast.cs

📁 报刊广告管理系统。CSharp编写
💻 CS
字号:
using System;
using System.IO;
using System.Data;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace WWAM
{
	/// <summary>
	/// 数据类型转换
	/// </summary>
	public class Cast
	{
		#region byte[]转换为
		public static string BytesToString(byte[] bytes)
		{
			return BytesToString(bytes,System.Text.Encoding.Default);
		}
		public static string BytesToString(byte[] bytes,System.Text.Encoding encoding)
		{
			return encoding.GetString(bytes);
		}


		public static void BytesToStream(Stream s,byte[] bytes)
		{
			s.Write(bytes,0,bytes.Length);
		}
		#endregion

		#region Stream转换为
		public static byte[] StreamTobytes(Stream s)
		{
			byte[] bytes = new byte[Convert.ToInt32(s.Length)];
			s.Position = 0;
			s.Read(bytes,0,Convert.ToInt32(s.Length));
			return bytes;
		}
		#endregion
		
		#region string转换为
		public static byte[] StringToBytes(string s)
		{
			return StringToBytes(s,System.Text.Encoding.Default);
		}
		public static byte[] StringToBytes(string s, System.Text.Encoding encoding)
		{
			if(s==null)
			{
				return new byte[0];
			}
			else
			{
				return encoding.GetBytes(s);
			}
		}
		#endregion
        	
		#region 数字转换为
		/// <summary>
		/// 数字转换为字符串
		/// </summary>
		/// <param name="d">数字</param>
		/// <param name="length">总长度(0表示不定限制)</param>
		/// <param name="digits">小数位(负数表示不限制)</param>
		/// <returns></returns>
		public static string DemicalToString(double d , int length , int digits)
		{
			if(length <= 0)
			{
				if (digits < 0)
				{
					return d.ToString();
				}
				else
				{
					return d.ToString("f"+digits.ToString());
				}
			}
			else
			{
				if (digits < 0)
				{
					return d.ToString();
				}
				else
				{
					length = Math.Max(((int)d).ToString().Length,length);
					length = Math.Min(d.ToString("f"+digits.ToString()).Length,length);
					return d.ToString("f"+digits.ToString()).Substring(0,length);
				}
			}
		}
		#endregion

		#region byte转换为
		#endregion
		
		#region char转换及相关
		public static bool FileNameUnusedChar(char c)
		{
			char[] unused = new char[]{'\x5c','\x2f','\x3a','\x29','\x3f','\x22','\x3c','\x3e','\x7c'};
			//string errorstring = "名称中不能出现下列字符之一\r\n\\ / : * ? \" < > |";
			return Array.IndexOf(unused,c)!=-1;
		}
		#endregion
		
		#region DateTime转换及相关
		/// <summary>
		/// 用两个DateTime型生成新的DateTime,取datePart的日期部分,取timePart的时间部分
		/// </summary>
		/// <param name="datePart">取其日期部分</param>
		/// <param name="timePart">取其时间部分</param>
		/// <returns></returns>
		public static DateTime DateTimeCoalition(DateTime datePart,DateTime timePart)
		{
			return datePart.Date.Add(timePart.TimeOfDay);
		}
		/// <summary>
		/// 用一个DateTime型生成新的DateTime,取datePart的日期部分,取系统当前时间的时间部分
		/// </summary>
		/// <param name="datePart">取其日期部分</param>
		/// <returns></returns>
		public static DateTime DateTimeCoalition(DateTime datePart)
		{
			return datePart.Date.Add(DateTime.Now.TimeOfDay);
		}
		#endregion

		#region Object转换为
		public static object ConvertObject(object o , System.Type fitType , object defaultValue)
		{
			if(o==null)
			{
				return defaultValue;
			}
			else
			{
				try
				{
					return Convert.ChangeType(o,fitType);
				}
				catch
				{
					return defaultValue;
				}
			}
		}
		#endregion

		#region 文件操作
		public static string SaveFileName(string fileName,string exName)
		{
			SaveFileDialog saver = new SaveFileDialog();
			if(exName=="")
			{
				saver.Filter = "所有文件|*.*";
			}
			else
			{
				saver.Filter = string.Format("*.{0}|*.{0}",exName);
			}
			saver.FileName = fileName;
			if(saver.ShowDialog()==DialogResult.OK)
			{
				return saver.FileName;
			}
			else
			{
				return "";
			}
		}
		public static string SaveFileName()
		{
			return SaveFileName("","");
		}
		public static byte[] ReadFromFile(string filepath)
		{
			try
			{
				FileStream file = new FileStream(filepath,FileMode.Open);
				byte[] filebytes = new byte[(int)file.Length];
				file.Read(filebytes,0,(int)file.Length);
				file.Flush();
				file.Close();
				return filebytes;
			}
			catch
			{
				return null;
			}
		}
		public static bool SaveToFile(string filepath,byte[] content,bool OverWrite)
		{
			try
			{
				if(File.Exists(filepath) && !OverWrite)
				{
					return false;
				}
				FileStream file = new FileStream(filepath,FileMode.Create);
				file.Write(content,0,content.Length);
				file.Flush();
				file.Close();
				return true;
			}
			catch(Exception ex)
			{
				string s = ex.Message;
				return false;
			}
		}

		public static bool SaveToFile(string filepath,string content,bool appended,bool writeLine)
		{
			try
			{
				using(StreamWriter w = new StreamWriter(filepath,appended))
				{
					if(writeLine)
					{
						w.WriteLine(content);
					}
					else
					{
						w.Write(content);
						w.Flush();
						w.Close();
					}
				}
				return true;
			}
			catch(Exception ex)
			{
				string s = ex.Message;
				return false;
			}

		}
		#endregion

		#region DataTable相关
		public static int[] DatasToInts(DataTable _dt, string fieldName)
		{
			if(_dt.Columns.Contains(fieldName))
			{
				if(_dt.Columns[fieldName].DataType == typeof(int))
				{
					int[] ints = new int[_dt.Rows.Count];
					for(int i=0; i<_dt.Rows.Count; i++)
					{
						ints[i] = (int)_dt.Rows[i][fieldName];
					}
					Array.Sort(ints);
					return ints;
				}
			}
			return new int[0]; 
		}
		public static int[] DatasToInts(DataTable _dt, int colNum)
		{
			if(_dt.Columns.Count>colNum && colNum >=0)
			{
				if(_dt.Columns[colNum].DataType == typeof(int))
				{
					int[] ints = new int[_dt.Rows.Count];
					for(int i=0; i<_dt.Rows.Count; i++)
					{
						ints[i] = (int)_dt.Rows[i][colNum];
					}
					Array.Sort(ints);
					return ints;
				}
			}
			return new int[0]; 
		}	
		#endregion
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -