读写的完整流程例子可参考:stream简单读写excel

写文件:

string demo="写入txt中";
   StreamWriter demo_log = null;
            demo_log = File.CreateText("demo_log.txt");
   demo_log.WriteLine(demo,Encoding.GetEncoding("gb2312"));

 demo_log.Close();



写后要释放,如果不释放会把这个文件锁住.:


demo_log.Dispose();








读是要有共享的方式读:


if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string str = null;

                FileStream fs = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                StreamReader reader = new StreamReader(fs, System.Text.Encoding.Default);
                while ((str = reader.ReadLine()) != null)
                {


                    // split the string
                    //MessageBox.Show(str);
                    string[] strs = str.Split('|');
                }
                reader.Close();
            }





读取图片的锁问题可以用克隆的方式解决:

// 通过生成clone的方式,使用clone来赋值,从而 FileSourcePath对应的图片得到解锁
//
System.Drawing.Image img = System.Drawing.Image.FromFile(FileSourcePath); 
System.Drawing.Image bmp = new System.Drawing.Bitmap(img); 
img.Dispose(); 


pictureBox1.Image = bmp; // System.Drawing.Image.FromFile(FileSourcePath); 
pictureBox1.Visible = true;