经常用到操作文件的时候,下面总结一些方法

                var filePath = Server.MapPath(item.Path);
                if (File.Exists(filePath))
                {
                    //删除文件 同时判断文件下面是否还有文件 没有则将目录页删除了
                    File.Delete(filePath);
                    var directoryPath = Path.GetDirectoryName(item.Path);
                    DirectoryInfo di = new DirectoryInfo(directoryPath);
                    if (di.GetFiles().Length == 0)
                    {
                        di.Delete();
                    }
                }

 

//当前文件所在的目录没有文件的时候 删除当前目录
                        var directoryPath = Path.GetDirectoryName(p.Path);
                        DirectoryInfo di = new DirectoryInfo(directoryPath);
                        if (di.GetFiles().Length == 0)
                        {
                            di.Delete();
                        }

 

posted @ 2016-09-06 09:55  麦田HH  阅读(184)  评论(0编辑  收藏  举报