c# 文件操作 判断文件是否为空的方法

如题所述

第1个回答  2017-03-10
protected void Page_Load(object sender, EventArgs e)
        {
            string path = string.Format(@"d:\123.txt");//需要引入using System.IO;
            if (File.Exists(path))
            {
                FileInfo fi = new FileInfo(path);
                int l = Convert.ToInt32(fi.Length);
                if (l == 0)
                {
                    Response.Write("<script>alert('目标文件存在,但内容为空!');</script>");                   
                }
                else
                {
                    Response.Write("<script>alert('目标文件存在,内容不为空!');</script>");                   
                }
            }
            else
            {
                File.Create(path);
                if (File.Exists(path))
                {
                    Response.Write("<script>alert('目标文件不存在,自动创建文件成功!');</script>");                  
                }
                else
                {
                    Response.Write("<script>alert('目标文件不存在,自动创建文件失败!');</script>");                }
            }
        }

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网