文件管理

以下的添刪改方法不是系統自帶的通過方法,而是我老大自寫的一個方法來實現的。

文件管理接口:

/*
 * 由SharpDevelop创建。
 * 用户: auyeungck
 * 日期: 2013/4/22
 * 时间: 下午 02:07
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using WebMRP.Controllers.Models;
using System.IO;
using System.Net;

namespace WebMRP.Controllers.common
{
    /// <summary>
    /// Description of IFileManager.
    /// </summary>
    public interface IFileManager
    {
        //添加文件
        Guid  AddFile(System.IO.Stream stream, FileInfo2  info);
        //查詢文件
       FileInfo2 QueryInfo(Guid guid);
        //刪除文件
        bool     DeleteFile(Guid guid);
        //查詢文件
        System.IO.Stream OpenFile(Guid fileGuid);
    }
}


實現文件管理接口類:

/*
 * 由SharpDevelop创建。
 * 用户: auyeungck
 * 日期: 2013/4/22
 * 时间: 下午 02:23
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System .IO;
using WebMRP.Controllers.Models;
using System.Collections.Generic;
using System.Net;
using System .Web ;
using QP.Component.Data;
using QP.Component.Data.Entity;
using System.Web.Mvc;
using System.Linq;
using System.Text;


namespace WebMRP.Controllers.common
{
    /// <summary>
    /// Description of FileManagers.
    /// </summary>
    public class FileManagers:IFileManager
    {
        #region 添加文件
        /// <summary>
        /// 添加文件
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="info"></param>
        /// <returns></returns>
           public  Guid  AddFile(System.IO.Stream stream, FileInfo2 info)
           {
               Guid guid=Guid.NewGuid ();
               string savePath = "D:\\auyeungck\\fileTestDown\\"+info.Filename; 
            WebMRP.Controllers.Models.FileInfo2 fileInfo=new WebMRP.Controllers.Models.FileInfo2();
                fileInfo.Guid =Convert .ToString(guid);
                fileInfo.Filename=info.Filename; 
                fileInfo.Filetype= info.Filetype;
                fileInfo.Size=info.Size;
                fileInfo .Storagerepos =savePath;
                fileInfo .Comment="說明";//說明 
                fileInfo.Hashkey="test1";//這裡的hash寫什麼?
                fileInfo.Datecreated=DateTime.Now;
                fileInfo .Delmark="N";
                fileInfo .Userid ="0256";
                var t=DAL.DalHelper.CreateCtx(); 
                fileInfo.ResetUpdate();
                 fileInfo.SetStatus(0, DataStatus.NewModified);                
                   var result=t.Save(fileInfo);
                   if ( result.State != DbExecuteState.Success && result.RowsAffected<=0)
                {
                    throw new Exception(QP.Component.Data.Utils.ObjectUtils.Dump(fileInfo));
                }
               
                   
                //以下是將文件保存到具體的一個文件夾中 
               
                byte[] buffer = new byte[1024];   
                Stream outStream = null;      
                Stream inStream = null;     
                try       
                {           
                    if (File.Exists(savePath))
                        File.Delete(savePath);
                    
                    outStream = System.IO.File.Create(savePath);   
                    inStream = stream;   
                    int l;          
                    do            
                    {              
                        l = inStream.Read(buffer, 0,buffer.Length);             
                        if (l > 0) outStream.Write(buffer, 0, l);       
                    } while (l > 0);             
                           
                } catch (Exception e) 
                {
                    throw e;
                }
                finally         
                {              
                    if(outStream!=null) outStream.Close();   
                    if(inStream!=null) inStream.Close();
                }
                   return guid ;
           }
           #endregion
           
          #region 查詢文件
        /// <summary>
        /// 查詢文件
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
           public FileInfo2 QueryInfo(Guid guid)
           {
               var t=DAL.DalHelper.CreateCtx();
               
               IEnumerable<WebMRP.Controllers.Models.FileInfo2> o= t.Load<WebMRP.Controllers.Models.FileInfo2>(m=>m.Guid ==Convert.ToString(guid ));
               FileInfo2 fileInfo=new FileInfo2 ();
               foreach (var i in o) {
                    fileInfo.Fileid =i.Fileid ;
                    fileInfo.Guid=i.Guid ;
                    fileInfo .Filename=i.Filename;
                    fileInfo .Filetype=i.Filetype;
                    fileInfo.Delmark=i.Delmark;
                    fileInfo .Datecreated=i.Datecreated;
                    fileInfo.Hashkey=i.Hashkey;
                    fileInfo.Comment=i.Comment;
                    fileInfo.Storagerepos=i.Storagerepos;
                    fileInfo.Userid=i.Userid;
                    fileInfo .Size=i.Size;
                }
               return fileInfo;
           }
           #endregion
          
          #region 刪除文件
        /// <summary>
        /// 刪除文件
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
           public  bool  DeleteFile(Guid guid)
           {
       
               var t=DAL.DalHelper.CreateCtx();
               var o= t.Load<FileInfo2>(m=>m.Guid ==Convert.ToString(guid)).FirstOrDefault();
               o.Delmark="Y";
               var result=t.Save(o);
                if ( result.State != DbExecuteState.Success && result.RowsAffected<=0)
            {
                throw new Exception(QP.Component.Data.Utils.ObjectUtils.Dump(o));
            }
               
               return true ;
           }
           #endregion
        
           #region 打開文件
        /// <summary>
        /// 打開文件
        /// </summary>
        /// <param name="fileGuid"></param>
        /// <returns></returns>
        public System.IO.Stream OpenFile(Guid fileGuid)
        {
            var t=DAL.DalHelper.CreateCtx();
               
               IEnumerable<WebMRP.Controllers.Models.FileInfo2> o= t.Load<WebMRP.Controllers.Models.FileInfo2>(m=>m.Guid ==Convert.ToString(fileGuid));
               FileInfo2 fileInfo=new FileInfo2 ();
               foreach (var i in o) {
                    fileInfo.Fileid =i.Fileid ;
                    fileInfo.Guid=i.Guid ;
                    fileInfo .Filename=i.Filename;
                    fileInfo .Filetype=i.Filetype;
                    fileInfo.Storagerepos=i.Storagerepos;
                    fileInfo .Datecreated=i.Datecreated;
                    fileInfo.Hashkey=i.Hashkey;
                    fileInfo.Comment=i.Comment;
                    fileInfo.Delmark=i.Delmark;
                    fileInfo.Userid=i.Userid;
                    fileInfo .Size=i.Size;
                }
            string path=fileInfo.Storagerepos;
               FileStream fs=new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            return  fs;
        }
        
        #endregion
    }
}


通過控制台進行測試:

/*
 * 由SharpDevelop创建。
 * 用户: auyeungck
 * 日期: 2013/4/23
 * 时间: 上午 09:26
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.IO;
using WebMRP.Controllers.common;
using System.Linq;
using WebMRP.Controllers.Models;

namespace test
{
    class Program
    {
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static Stream OpenFile(string filename)
        {
            return new FileStream(filename,FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
        }
        /// <summary>
        /// 添加文件
        /// </summary>
        /// <param name="filename"></param>
        public static void  TestAdd(string filename)
        {
            
            IFileManager fileManager=new FileManagers ();
            
            Guid guid;
            FileInfo2 info=new FileInfo2()
            {
                Size=123456,
                Filename=filename.Substring(5),
                Filetype=filename.Substring(filename.IndexOf('.')+1)
            };
            
            var fs = OpenFile(filename);
        
            Console .WriteLine ("TestAddFile");
            guid = fileManager.AddFile(fs,info);
            Console .WriteLine ("TestQueryInfo:");
            TestQueryInfo(guid);
            Console .WriteLine ("TestDeleteFile:");
            TestDeleteFile(guid);
            Console .WriteLine ("TestOpenFile:");
            TestOpenFile(guid);
            
            
            
            
        }
        /// <summary>
        /// 測試查詢文件
        /// </summary>
        public static void TestQueryInfo(Guid guid)
        {
            IFileManager fileManager=new FileManagers ();
            //Guid guid=new Guid ("5ac8d6dc-b04d-47b6-b51b-7dd4f2dbb3d4");
            FileInfo2 o=new FileInfo2 ();
                o=fileManager.QueryInfo(guid);
            Console .WriteLine ("Fileid:"+o.Fileid);
            Console .WriteLine ("Filename:"+o.Filename);
            Console .WriteLine ("Size:"+o.Size);
            Console .WriteLine ("Filetype:"+o.Filetype);
            Console .WriteLine ("Comment:"+o.Comment);
            Console .WriteLine ("Hashkey:"+o.Hashkey);
            Console .WriteLine ("Datecreated:"+o.Datecreated);
            Console .WriteLine ("Delmark:"+o.Delmark);
            Console .WriteLine ("Guid:"+o.Guid);
            Console .WriteLine ("Userid:"+o.Userid);
        }
        /// <summary>
        /// 刪除文件
        /// </summary>
        public static void TestDeleteFile(Guid guid)
        {
            IFileManager fileManager=new FileManagers ();
            //Guid guid=new Guid ("443cadf2-65cd-46ca-b966-9e61120f5333");
            bool isOk=fileManager.DeleteFile(guid);
            if(isOk)
            {
                Console.WriteLine ("success");
            }
            else
            {
                Console .WriteLine ("失敗");
            }
        }
        
        /// <summary>
        /// 測試打開文件
        /// </summary>
        public static void  TestOpenFile(Guid guid)
        {
            IFileManager fileManager=new FileManagers ();
            
            //Guid guid=new Guid ("443cadf2-65cd-46ca-b966-9e61120f5333");
            
            Stream  stream=fileManager.OpenFile(guid);
            
            
             //以下是將文件保存到具體的一個文件夾中 
             
            
            FileInfo2 o=new FileInfo2 ();
                o=fileManager.QueryInfo(guid);
               string savePath = "D:\\auyeungPhoto\\testFile\\"+o.Filename; 
                byte[] buffer = new byte[1024];   
                Stream outStream = null;      
                Stream inStream = null;     
                try       
                {           
                    if (File.Exists(savePath))
                        File.Delete(savePath);
                    
                    outStream = System.IO.File.Create(savePath);   
                    inStream = stream;   
                    int l;          
                    do            
                    {              
                        l = inStream.Read(buffer, 0,buffer.Length);             
                        if (l > 0) outStream.Write(buffer, 0, l);       
                    } while (l > 0);             
                           
                }        
                finally         
                {              
                    if(outStream!=null) outStream.Close();   
                    if(inStream!=null) inStream.Close();
                 }
                Console .WriteLine ("打開文件調用完成,並將保存到D:\\auyeungPhoto\\testFile\\下");
        }
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            
            // TODO: Implement Functionality Here
            
            /*System.IO.Directory.GetFiles(@"c:\").All(
                file=>TestAdd(file);
            
                return true;
            
            );*/
            
            //TestDeleteFile();//ok
              //TestQueryInfo();//ok
              //TestOpenFile();//ok
            
            string[] array1 = Directory.GetFiles(@"D:\1\");

            // Put all txt files in root directory into array.
            string[] array2 = Directory.GetFiles(@"C:\""*.BIN"); 
        
            //array1=new String[]{@"D:\1\TestDriven.NET.zip"};
            // Display all files.
            Console.WriteLine("--- Files: ---");
            foreach (string name in array1)
            {
                try {
                        TestAdd(name);
                } catch (Exception e) {
                    
                    //throw;
                    Console.WriteLine(string.Format("=>{0}\r\n***{1}", name, e.ToString()));
                    Console.ReadKey();
                    
                }
                
                Console.WriteLine(name);
            }
        
            // Display all BIN files.
            Console.WriteLine("--- BIN Files: ---");
            foreach (string name in array2)
            {
                Console.WriteLine(name);
            }
            
            
            string a = "c:\\test.txt";
            string news=a.Substring(a.IndexOf('.')+1);
            Console.Write("substring:");
                Console.WriteLine(news);
           
              
              
                  
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值