網頁

顯示具有 C# 標籤的文章。 顯示所有文章
顯示具有 C# 標籤的文章。 顯示所有文章

2010年3月15日 星期一

C#, 檔案是否存在,資料夾是否存在

加入參考
using System.IO;

判斷目錄是否存在

//判斷C槽是否存在
if (Directory.Exists("C:"))
{
//有
}

判斷檔案是否存在

//判斷執行檔路徑裡檔案CMD.txt是否存在
if (System.IO.File.Exists(Application.StartupPath + @"\COM.TXT"))
{
//有
}

C#儲存圖檔-小畫家3

儲存圖檔之前要先知道要儲存的檔案格式(BMP JPG GIF PNG)
而儲存圖檔的方式也很簡單,只要對BitMap 物件作Save 就可以
因為之前我們的範例都是使用BitMap 當作PictureBox的 Buffer
現在只要對Buffer 做SAVE就是把PICTUER 的圖形做儲存,
得知現在儲存的圖形格式有兩種方式,第一個方式是 對話方塊的過慮器
           saveFileDialog1.Filter = "逼恩批|*.bmp|傑批居|*.jpg|批恩居|*.png"; //檔案過濾器  
saveFileDialog1.InitialDirectory = Application.StartupPath; //程式啟動路徑,為預設路徑
saveFileDialog1.FileName = ""; //清除預設檔名
saveFileDialog1.ShowDialog(); //顯示對話方塊

if (openFileDialog1.FileName.Equals(""))
{
MessageBox.Show("取消選取");
}
else
{
switch (saveFileDialog1.FilterIndex)
{
case 1:
buffer.Save(saveFileDialog1.FileName , System.Drawing.Imaging.ImageFormat.Bmp);
break;
case 2:
buffer.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg );
break;
case 3:
buffer.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Gif );
break;
default:
break;
}
}

第二種方式,取出字串
再由字串比對

saveFileDialog1.Filter = "逼恩批|*.bmp|傑批居|*.jpg|批恩居|*.png"; //檔案過濾器  
saveFileDialog1.InitialDirectory = Application.StartupPath; //程式啟動路徑,為預設路徑
saveFileDialog1.FileName = ""; //清除預設檔名
saveFileDialog1.ShowDialog(); //顯示對話方塊

if (openFileDialog1.FileName.Equals(""))
{
MessageBox.Show("取消選取");
}
else
{
switch (saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.Length - 3, 3).ToUpper())
{
case "BMP":
buffer.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
break;
case "JPG":
buffer.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case "GIF":
buffer.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Gif );
break;
case "PNG":
buffer.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Png );
break;
default:
MessageBox.Show ("錯誤檔名");
break;
}
}
看起來是第一種寫法比較好,第二種寫法單存練習,如何取出最後三個字元,然後將其大寫

2010年3月14日 星期日

C#讀取圖形-小畫家2

C#讀取圖形有點意外的簡單
於上一個章節已經學會對會方塊,現在我們使用對話方塊來讀取圖形
Graphics g = this.pictureBox1.CreateGraphics();
Bitmap br = new Bitmap(openFileDialog1.FileName) ;
//放置您所指定的圖片
//並指定圖片要放置的位置,(X,Y) = (0,0)
g.DrawImage(br, 0, 0);
支援的圖檔名稱還真不少,GIF.JPG.JPEG.BMP.WMF.PNG

不過這樣寫有個問題,就是畫面會消失,其他視窗蓋到畫面,圖形就被吃掉了,
用一個簡單的方法解決,建立一個Bimmap 對應到PictureBox.Image
只要PictureBox 被其他視窗擋到,PictureBox 自動重繪的時候就會去Image抓圖,

    public partial class Form1 : Form
    {
        Bitmap buffer;
        public Form1()
        {
            InitializeComponent();
            buffer = new Bitmap(pictureBox1.Width, pictureBox1.Height ); 
        }
        private void OpenFileButton_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "逼恩批|*.bmp|傑批居|*.jpg|批恩居|*.png"; //檔案過濾器
            openFileDialog1.Multiselect = false;                  //取消多從選擇 = 單選   
            openFileDialog1.InitialDirectory = Application.StartupPath;   //程式啟動路徑,為預設路徑
            openFileDialog1.FileName = "";                              //清除預設檔名
            openFileDialog1.ShowDialog();                                    //顯示對話方塊
            if  (openFileDialog1.FileName.Equals( "" ))
            {
                MessageBox.Show("取消選取" );  
            }
            else
            {
                buffer = new Bitmap(openFileDialog1.FileName);
                //放置您所指定的圖片
                //並指定圖片要放置的位置,(X,Y) = (0,0)
                pictureBox1.Width = buffer.Width;
                pictureBox1.Height = buffer.Height;
                pictureBox1.Image = buffer;
            }
        }
    }

C#對話方塊

我是一個不會C#的人
跟著書去學C# 很無趣,於是給自己定了一個目標,寫一個小畫家
第一部先建立一個介面,先學習怎麼使用C# 工作列
工作列這樣就可以了,使用起來就像是按鈕一樣。接著學習怎麼呼叫 WINDOWS的開啟檔案對話方塊
openFileDialog1.Filter = "逼恩批|*.bmp|傑批居|*.jpg|批恩居|*.png"; //檔案過濾器
openFileDialog1.Multiselect = false; //取消多從選擇 = 單選
openFileDialog1.InitialDirectory = Application.StartupPath; //程式啟動路徑,為預設路徑
openFileDialog1.FileName = ""; //清除預設檔名
openFileDialog1.ShowDialog(); //顯示對話方塊 if (openFileDialog1.FileName.Equals( "" ))
{
MessageBox.Show("取消選取" );
}
else
{
MessageBox.Show(openFileDialog1.FileName );
}

C#用起來就是這麼有成就感。

C#二進位檔讀取 存檔 文字檔讀取 存檔

對於檔案的處理,很多人都很害怕,特別是二進位存取,
聽到二進位的存取就莫名的害怕,
不過於C#這些東西都變的非常的簡單,因為C#的函數都幫使用者作好了
不需要瞭解太多,只需要會用就可以,至於怎麼撰寫不再多說因為書上很多
今天要說的是整理成一些方便的函數

成功失敗 = 讀取二進位檔( 路徑檔名 , ref 讀取的結果 )
成功失敗 = 寫入二進位檔( 路徑檔名 , 寫入的陣列 )
成功失敗 = 讀取文字檔( 路徑檔名 , ref 讀取的文字 )
成功失敗 = 寫入文字檔( 路徑檔名 , 寫入的文字 )

這裡大概有些人會驚訝,如果檔案很大,那陣列或是字串會不會死掉,
在C#裡的陣列大小可以定到 2G,也就是檔案如果小於2G都可以讀取近來
ㄧ個String的變數其實可以放到記憶體爆掉,不過當然我們不會做這麼危險的事情
但是把ㄧ整本書的文字都放在一個string的變數裡是輕而ㄧ舉的喔

所以就大膽的用下去,




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace AkingTool
{
class FileIOTool
{
public static int BinWrite(string SaveFileName, byte[] InData)
{
try
{
//開啟建立檔案
FileStream myFile = File.Open(SaveFileName, FileMode.Open , FileAccess.ReadWrite);
BinaryWriter myWriter = new BinaryWriter(myFile);
myWriter.Write(InData);
myWriter.Close();
myFile.Close();
return 1;
}catch (InvalidCastException e)
{
return -1;
}

}

public static int BinRead(string OpenFileName, ref byte[] InData)
{

try
{
//開啟檔案
FileStream myFile = File.Open(OpenFileName, FileMode.Open , FileAccess.ReadWrite);
//引用myReader類別
BinaryReader myReader = new BinaryReader(myFile);
int dl = System.Convert.ToInt32 (myFile.Length);
//讀取位元陣列
InData = myReader.ReadBytes(dl);
//讀取資料
//釋放資源
myReader.Close();
myFile.Close();
return 1;
}
catch (InvalidCastException e)
{
return -1;
}
}

public static int StringWrite(string SaveFileName, string InData)
{
try
{
//建立檔案
FileStream myFile = File.Open(SaveFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamWriter myWriter = new StreamWriter(myFile);
//建立位元陣列
myWriter.Write(InData);
//釋放資源
myWriter.Close();
myFile.Close();
return 1;
}
catch (InvalidCastException e)
{
return -1;
}
}

public static int StringRead(string OpenFileName, ref string InData)
{
try
{
//開啟檔案
FileStream myFile = File.Open(OpenFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
//引用myReader類別
StreamReader myReader = new StreamReader(myFile);
int dl = System.Convert.ToInt32(myFile.Length);
//讀取位元陣列
InData = myReader.ReadToEnd();
//讀取資料
//釋放資源
myReader.Close();
myFile.Close();
return 1;
}
catch (InvalidCastException e)
{
return -1;
}
}
}
}
寫個範例讀寫一下



private void button1_Click(object sender, EventArgs e)
{
string G;
G = "abcdefghijklmnopqrstuvwxyz\nabcdefghijklmnopqrstuvwxyz";
AkingTool.FileIOTool.StringWrite(@"c:\kkk", G);
}

private void button2_Click(object sender, EventArgs e)
{

string K = "";
AkingTool.FileIOTool.StringRead(@"c:\kkk",ref K);
MessageBox.Show(K);
}

private void button3_Click(object sender, EventArgs e)
{
byte[] S = new byte[1024 * 1024]; //寫出1M的檔案
S[10000] = 200;
AkingTool.FileIOTool.BinWrite(@"C:\Bin.bin", S);
}

private void button4_Click(object sender, EventArgs e)
{
byte[] ReadS = new byte[0]; //'讀取1M 的檔案,大小先設定為0 後來自動調整為 1M
AkingTool.FileIOTool.BinRead (@"C:\Bin.bin",ref ReadS);
MessageBox.Show ( ReadS[10000].ToString() );
}