using System.IO;
判斷目錄是否存在
//判斷C槽是否存在
if (Directory.Exists("C:"))
{
//有
}
判斷檔案是否存在
//判斷執行檔路徑裡檔案CMD.txt是否存在
if (System.IO.File.Exists(Application.StartupPath + @"\COM.TXT"))
{
//有
}
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;
}
}
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;
}
}
}
寫個範例讀寫一下
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() );
}