Click here to Skip to main content
15,896,726 members

creend - Professional Profile



Summary

    Blog RSS
45
Authority
-22
Debator
0
Enquirer
32
Organiser
389
Participant
0
Author
0
Editor
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Reputation

Weekly Data. Recent events may not appear immediately. For information on Reputation please see the FAQ.

Privileges

Members need to achieve at least one of the given member levels in the given reputation categories in order to perform a given action. For example, to store personal files in your account area you will need to achieve Platinum level in either the Author or Authority category. The "If Owner" column means that owners of an item automatically have the privilege. The member types column lists member types who gain the privilege regardless of their reputation level.

ActionAuthorAuthorityDebatorEditorEnquirerOrganiserParticipantIf OwnerMember Types
Have no restrictions on voting frequencysilversilversilversilver
Bypass spam checks when posting contentsilversilversilversilversilversilvergoldSubEditor, Mentor, Protector, Editor
Store personal files in your account areaplatinumplatinumSubEditor, Editor
Have live hyperlinks in your profilebronzebronzebronzebronzebronzebronzesilverSubEditor, Protector, Editor
Have the ability to include a biography in your profilebronzebronzebronzebronzebronzebronzesilverSubEditor, Protector, Editor
Edit a Question in Q&AsilversilversilversilverYesSubEditor, Protector, Editor
Edit an Answer in Q&AsilversilversilversilverYesSubEditor, Protector, Editor
Delete a Question in Q&AYesSubEditor, Protector, Editor
Delete an Answer in Q&AYesSubEditor, Protector, Editor
Report an ArticlesilversilversilversilverSubEditor, Mentor, Protector, Editor
Approve/Disapprove a pending ArticlegoldgoldgoldgoldSubEditor, Mentor, Protector, Editor
Edit other members' articlesSubEditor, Protector, Editor
Create an article without requiring moderationplatinumSubEditor, Mentor, Protector, Editor
Approve/Disapprove a pending QuestionProtector
Approve/Disapprove a pending AnswerProtector
Report a forum messagesilversilverbronzeProtector, Editor
Approve/Disapprove a pending Forum MessageProtector
Have the ability to send direct emails to members in the forumsProtector
Create a new tagsilversilversilversilver
Modify a tagsilversilversilversilver

Actions with a green tick can be performed by this member.


 
GeneralSpringGraphRoamer Pin
creend17-Nov-11 20:06
creend17-Nov-11 20:06 
Generalencoding2 Pin
creend9-Feb-11 21:07
creend9-Feb-11 21:07 
Generalencoding Pin
creend9-Feb-11 16:16
creend9-Feb-11 16:16 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace CharFormatConversion
{
public partial class MainForm : Form
{
static string msg = "将开始转换操作!如果您选择了备份选项,将在文件当前目录下生成 【文件名_backup.后缀名】备份文件\n不过还是建议您,将当前转换的文件或目录压包备份下吧,我不能保证程序会突然出现个灵异的情况\n因为如果没有BUG的软件已经不能叫软件了!\n转换后出现任何让你惊讶的情况,本人可概不负责哦~\n点击OK转换,点击Cancel退出!";

#region 变量

private string newFormat = null;

private bool isBackUp = false;

private static string[] filterString = null;

public static string[] FilterString
{
get { return MainForm.filterString; }
set { MainForm.filterString = value; }
}
#endregion

#region 构造
public MainForm()
{
InitializeComponent();
}

public MainForm(string [] args)
{
InitializeComponent();

add(args);
}
#endregion

#region 多个文件夹或目录添加
private void add(string[] args)
{
int i;
int length = args.Length;
for (i = 0; i < length; i++)
{
string path = args[i];
ContentType type = judgeFileOrFolder(path);
switch (type)
{
case ContentType.FILE:
addfile(path);
break;
case ContentType.FOLDER:
if (showFilter(path))
adddirectory(path);
break;
}
}
}
#endregion

#region 判断是文件还是文件夹
private ContentType judgeFileOrFolder(string args)
{
FileInfo fileInfo = new FileInfo(args);
return fileInfo.Attributes == FileAttributes.Directory ? ContentType.FOLDER : ContentType.FILE;
}
#endregion

#region 退出
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
#endregion

#region 转换按钮
private void btnConvert_Click(object sender, EventArgs e)
{
if (listBox.Items.Count != 0)
{
if (MessageBox.Show(msg, "操作提示!!!!!", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
getIsBackUP();
getFormat();
try
{
convert();
MessageBox.Show("转换成功!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
listBox.Items.Clear();
}
catch (Exception exception)
{
MessageBox.Show("转换失败!\n" + exception.Message, "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
else
{
DialogResult reault = MessageBox.Show("还未添加任何文件!【是】 添加文件 【否】 添加目录 ", "操作提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
switch (reault)
{
case DialogResult.Yes:
btnAddFile_Click(null,null);
break;
case DialogResult.No:
btnAddFolder_Click(null,null);
break;
case DialogResult.Cancel:
break;
}
}
}
#endregion

#region 转换主方法
private void convert()
{
foreach (string strPath in listBox.Items)
{
if (isBackUp)
{
backup(strPath);
}
FileStream stream = new FileStream(strPath, FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(strPath, GetEncodingType(stream));
string str = reader.ReadToEnd();
stream.Close();
reader.Close();

StreamWriter writer = null;
switch (newFormat)
{
case "GB2312":
writer = new StreamWriter(strPath, false, Encoding.Default);
break;
case "UTF8":
writer = new StreamWriter(strPath, false, Encoding.UTF8);
break;
case "UTF8_NO_BOM":
writer = new StreamWriter(strPath, false, new UTF8Encoding(false));
break;
}
writer.Write(str);
writer.Close();
}
}
#endregion

#region 给定文件的路径,读取文件的二进制数据,判断文件的编码类型(未用到,放这备忘)
public Encoding GetType(string FILE_NAME)
{
FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
Encoding r = GetEncodingType(fs);
fs.Close();
return r;
}
#endregion

#region 通过给定的文件流,判断文件的编码类型
public Encoding GetEncodingType(FileStream fs)
{
//byte[] Unicode = new byte[] { 0xFF, 0xFE, 0x41 };
//byte[] UnicodeBIG = new byte[] { 0xFE, 0xFF, 0x00 };
//byte[] UTF8 = new byte[] { 0xEF, 0xBB, 0xBF }; //带BOM

Encoding reVal = Encoding.Default;

BinaryReader r = new BinaryReader(fs, Encoding.Default);
int i;
int.TryParse(fs.Length.ToString(), out i);
byte[] ss = r.ReadBytes(i);
if (IsUTF8Bytes(ss) || (ss[0] == 0xEF && ss[1] == 0xBB && ss[2] == 0xBF))
{
reVal = Encoding.UTF8;
}
else if (ss[0] == 0xFE && ss[1] == 0xFF && ss[2] == 0x00)
{
reVal = Encoding.BigEndianUnicode;
}
else if (ss[0] == 0xFF && ss[1] == 0xFE && ss[2] == 0x41)
{
reVal = Encoding.Unicode;
}
r.Close();
return reVal;
}
#endregion

#region 判断是否是不带 BOM 的 UTF8 格式
private bool IsUTF8Bytes(byte[] data)
{
int charByteCounter = 1;  //计算当前正分析的字符应还有的字节数
byte curByte; //当前分析的字节.
int length = data.Length;
int i;
for (i = 0; i < length; i++)
{
curByte = data[i];
if (charByteCounter == 1)
{
if (curByte >= 0x80)
{
//判断当前
while (((curByte <<= 1) & 0x80) != 0)
{
charByteCounter++;
}
//标记位首位若为非0 则至少以2个1开始 如:110XXXXX...........1111110X 
if (charByteCounter == 1 || charByteCounter > 6)
{
return false;
}
}
}
else
{
//若是UTF-8 此时第一位必须为1
if ((curByte & 0xC0) != 0x80)
{
return false;
}
charByteCounter--;
}
}
if (charByteCounter > 1)
{
throw new Exception("非预期的byte格式");
}
return true;
}
#endregion

#region 备份
private void backup(string path)
{
try
{
string copyPath = Path.Combine(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path) + "_backup" + Path.GetExtension(path));
File.Copy(path, copyPath, true);
}
catch (Exception ex)
{
MessageBox.Show("备份失败!\n"+ex.Message,"操作提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
#endregion

#region 是否备份
private void getIsBackUP()
{
if (chkBackup.Checked)
isBackUp = true;
else
isBackUp = false;
}
#endregion

#region 获取转换编码格式
private void getFormat()
{
System.Windows.Forms.Control.ControlCollection c = groupBox_new.Controls;
foreach (Control control in c)
{
if (((RadioButton)control).Checked)
{
newFormat = ((RadioButton)control).Text;
}
}
}
#endregion

#region 添加文件主方法
private void addfile(string filepath)
{
listBox.Items.Add(filepath);
}
#endregion

#region 添加目录主方法
private void adddirectory(string directoryarray)
{
DirectoryInfo directiry=null;
FileInfo[] fileInfo =null;
foreach (string str in filterString)
{
directiry = new DirectoryInfo(directoryarray);
fileInfo = directiry.GetFiles("*." + str, SearchOption.AllDirectories);
foreach (FileInfo info in fileInfo)
{
listBox.Items.Add(info.FullName);
}
directiry = null;
fileInfo = null;
}
}
#endregion

#region 添加文件按钮
private void btnAddFile_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
int i;
string [] folderPath = openFileDialog1.FileNames;
int length = folderPath.Length;

for (i = 0; i < length; i++)
{
addfile(folderPath[i]);
}
}
}
#endregion

#region 添加目录按钮
private void btnAddFolder_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
if (showFilter(""))
{
adddirectory(folderBrowserDialog1.SelectedPath);
}
}
}
#endregion

#region 显示Filter窗体
private bool showFilter(string str)
{
Filter f =null;
if (str.Length == 0)
f = new Filter();
else
f = new Filter(str);
return f.ShowDialog() == DialogResult.OK?true:false;
}
#endregion

#region 删除选中
private void btnDelSelected_Click(object sender, EventArgs e)
{
if (listBox.Items.Count != 0)
{
if (listBox.SelectedItems.Count != 0)
{
ListBox.SelectedObjectCollection collection = listBox.SelectedItems;
int count = collection.Count;
int i;
for (i = 0; i < count; i++)
{
listBox.Items.Remove(collection[0]);
}
}
else
{
MessageBox.Show("未选中任何记录!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
#endregion

#region 删除全部
private void btnDelAll_Click(object sender, EventArgs e)
{
if (listBox.Items.Count != 0)
{
listBox.Items.Clear();
}
}
#endregion

#region 关于
private void btnAbout_Click(object sender, EventArgs e)
{
AboutBox1 a = new AboutBox1();
a.ShowDialog();
}
#endregion

#region 窗体登陆
private void MainForm_Load(object sender, EventArgs e)
{
listBox.SelectionMode = SelectionMode.MultiExtended;
this.openFileDialog1.Filter = "文本文件|*.txt|Java 文件|*.java|C# 文件|*.cs|网页文件|*.html|网页文件|*.htm|样式表文件|*.css|JavaScript文件|*.js|所" +
"有文件|*.*";
}
#endregion

#region 窗体拖放事件
private void MainForm_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Link;
else
e.Effect = DragDropEffects.None;
}

private void MainForm_DragDrop(object sender, DragEventArgs e)
{
add((string[])e.Data.GetData(DataFormats.FileDrop));
}
#endregion
}
}
Generalwindow.open post Pin
creend5-Jul-10 22:29
creend5-Jul-10 22:29 
Generalsc Pin
creend3-Nov-09 19:40
creend3-Nov-09 19:40 
GeneralRe: sc Pin
creend28-Jan-10 16:38
creend28-Jan-10 16:38 
GeneralTJ2R3-WHW22-B848T-B78YJ-HHJWJ Pin
creend9-Sep-09 21:32
creend9-Sep-09 21:32 
Generalhttp://go2.microsoft.com/fwlink/?LinkID=87096&amp;errorID=1603&amp;component=dev Pin
creend26-Jul-09 19:43
creend26-Jul-09 19:43 
GeneralAboutScript Pin
creend1-Jul-09 23:16
creend1-Jul-09 23:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.