C#学习,新手总结的一点点技巧

作者:DoItNow

◎把字符串转化为字节流

byte[] bwrite=Encoding.GetEncoding("GB2312").GetBytes("中国1234".ToCharArray());
MessageBox.Show(bwrite.Length.ToString());
for(int i=0;i<bwrite.Length;i++)MessageBox.Show(bwrite[i].ToString());

◎把字符串写入文件

FileStream fs1=new FileStream(".\\test.dll",FileMode.OpenOrCreate);
byte[] ch=Encoding.Default.GetBytes(" 我爱北京天安门!\r\n".ToCharArray());
fs1.Write(ch,0,ch.Length); fs1.Close();

◎把字节流转化为字符串

string str;
byte[] bStr=new byte[3]{48+7,48+8,48+9};
str=Encoding.Default.GetString(bStr);
MessageBox.Show(str);

◎字符串转化为其base64编码

string s="cuidexuan";
byte[] by=Encoding.Default.GetBytes(s.ToCharArray());
s=Convert.ToBase64String(by);
MessageBox.Show(s);

◎有关文件名的处理

string s=Path.GetTempPath();

线程
ms-help://MS.VSCC/MS.MSDNVS.2052/cpguide/html/cpconthreading.htm
and
ms-help://MS.VSCC/MS.MSDNVS.2052/cpsamples/html/threading.htm

◎选择一个目录

public class FolderBrowser : System.Windows.Forms.Design.FolderNameEditor
{
    public string ShowDialog()
    {
        FolderBrowser fb = new FolderBrowser();
        fb.Description = "请选择一个目录:";
        //fb.Style=FolderNameEditor.FolderBrowserStyles.RestrictToFilesystem;
        fb.ShowDialog();
        string dirPath = fb.DirectoryPath;
        fb.Dispose();
        return dirPath;
    }
}

◎复制目录

private int dirCopy(string dirFrom, string dirTo)
{
    if (!Directory.Exists(dirFrom)) throw (new Exception(dirFrom + " not exists"));
    if (Directory.Exists(dirTo)) throw (new Exception(dirTo + " exists"));
    Directory.CreateDirectory(dirTo);
    try
    {
        foreach (string f in Directory.GetFiles(dirFrom))
        {
            File.Copy(f, dirTo + "\\" + Path.GetFileName(f));
        }
        foreach (string d in Directory.GetDirectories(dirFrom))
        {
            dirCopy(d, dirTo + "\\" + Path.GetFileName(d));
        }
        return 0;
    }
    catch (Exception) { return -1; }
}
Powered by DvNews.net
来源:UNCJ.NET
阅读:574 次
日期:2003-7-4
Contributors: FHL