File &Directory - 读取、写入等

1、Demo1

https://blog.csdn.net/baidu_39486224/article/details/79980758

//===================================================文件创建、移动、删除、生成、压缩
        private void button5_Click(object sender, EventArgs e)
        {
            string VerPath = "";
            string FromVerPath = "";
            string SFPath = "";
            string STemp = "", ToPath = "";
            NewFileCrePath = Path.Combine(LPath, SelectDir);
            try
            {
                if (!Directory.Exists(NewFileCrePath))
                {
                    Directory.CreateDirectory(NewFileCrePath);//创造一个新文件夹
                                                       //MessageBox.Show(sf.ToString());
                    VerPath = NewFileCrePath + @"\Version.txt";
                    FileStream fcreate = File.Create(VerPath);//创造一个版本记录文件
                    fcreate.Close();
                    for (int sfs = 0; sfs < CountFile; sfs++)//sf是个数,从1开始,sfs是循环计数,从0开始
                    {
                        STemp = SFiles[sfs];
                        STemp = STemp.Substring(0, STemp.Length - 4);//去掉文件名称后缀
                        SFPath = LPath + STemp + @"\" + STemp;//将要移动的文件夹   
                        ToPath = NewFileCrePath + @"\" + STemp;//目标文件夹
                        if (Directory.Exists(SFPath))
                        {
                            Directory.Move(SFPath, ToPath);//ToPath要设置为移动到这里的那个文件夹的名字(或者重命名
                            SFPath = LPath + STemp;
                            Directory.Delete(SFPath, true);
                        }
                        else
                        {
                            MessageBox.Show("要移动的文件不存在!");
                            return;
                        }
                        //=============================版本生成
                        FromVerPath = NewFileCrePath + @"\" + STemp + @"\Version.txt";
 
                        FileStream fsver = new FileStream(FromVerPath, FileMode.Open);//吧每一个文件的版本提取
                        StreamReader reader = new StreamReader(fsver, UnicodeEncoding.GetEncoding("GB2312"));//中文、读取
 
                        String[] str = new String[5];
                        for (int i = 0; i < 2; i++)
                        {
                            str[i] = reader.ReadLine();//赋值                     
                        }
                        StreamWriter sw = new StreamWriter(VerPath, true, UnicodeEncoding.GetEncoding("GB2312"));//写入
                        str[0] = STemp + "                   " +"\t "+ str[0];
                        sw.WriteLine(str[0]);//在后面写入
                        sw.Close();
                        fsver.Close();
 
                    }//for循环结束
                    CountFile = 0;
                    string newzip = NewFileCrePath + ".zip";
                    ZipDirectory(NewFileCrePath, newzip);//压缩一下
                    // FileUpLoad(newzip,"");上传一下
                }
                else
                {
                    MessageBox.Show("要创建的文件夹已经存在!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("error!");
                throw ex;
            }
 
        }
View Code

1、FileStream.close()的作用:就是关闭文件流。

https://zhidao.baidu.com/question/323934325.html
如果你开启了文件流在读取一个文件却没有close,那么这个文件一直被你的程序在占用。别人就无法再操作这个文件。(当然,读还是可以的,只是无法写这个文件
比如,你有两个程序,同时读取一个文件,并且不close,那么无论哪个程序都无法修改或删除这个文件。
我看了你的追问,关闭程序就可以不占用内存。是的,关闭程序就把内存给释放了,可问题是,你这个程序是短暂的寿命吗?你开发程序出来是给别人用的吧,那么这个程序就会一直开启状态,你不能随便去关闭它。比如,一个网站,那是一直开启放在互联网上的。一个游戏,一直开启给玩家玩的。你可以经常去把这个程序关闭吗?无论是网站还是游戏,都会有很多用户访问你的程序,如果不做到释放内存,100个用户就要占用100个文件流,1000个用户占用1000个,你的程序和服务器能够支撑不住那么多信息的。所以要做到及时释放内存,保证程序能够正常运行。

虽然说得有理,不过并不妨碍写入txt,比如你打开了txt,可是仍然能写入;

FileStream fCreate=File.Create(path1);

//fCreate.Close();//这里也没有关闭,后面也能够写入

FileStream fs1 = new FileStream(path1, FileMode.Append);
StreamWriter sw1 = new StreamWriter(fs1);
sw1.WriteLine("1234");
sw1.Flush();
sw1.Close();
fs1.Close();
Console.WriteLine("1234");

猜你喜欢

转载自www.cnblogs.com/wllwqdeai/p/11070392.html