zoukankan      html  css  js  c++  java
  • 对计算机上的文件夹进行操作

            /// <summary>
            /// 下载文件(将服务器上的文件copy到本地)
            /// </summary>
            /// <param name="localPath_Str">本地路径</param>
            /// <param name="sourcePath">服务器上的文件资源路径</param>
            void getDirs(string localPath_Str, string sourcePath)
            {
                string[] fileNames = Directory.GetFiles(sourcePath);//获得文件夹内的文件数量
                if (fileNames.Length > 0)
                {
                    for (int i = 0; i < fileNames.Length; i++)
                    {
                        int k = fileNames[i].LastIndexOf("\");
                        string fileName_Str = fileNames[i].Substring(k + 1);
                        if (!File.Exists(localPath_Str + "\" + fileName_Str))//检查本地是否存在将要下载的文件
                        {
                            File.Create(localPath_Str + "\" + fileName_Str).Close();//创建文件                  
                        }
                        File.Copy(fileNames[i], localPath_Str + "\" + fileName_Str, true);//替换掉文件内容。
                    }
                }
                string[] dirInfo = Directory.GetDirectories(sourcePath);//检查是否有文件夹
                if (dirInfo.Length > 0)
                {
                    for (int i = 0; i < dirInfo.Length; i++)
                    {
                        int k = dirInfo[i].LastIndexOf("\");
                        string dirInfo_Str = dirInfo[i].Substring(k + 1);
                        if (!Directory.Exists(localPath_Str + "\" + dirInfo_Str))//检查本地是否有相同的文件夹
                        {
                            DirectoryInfo directory = new DirectoryInfo(localPath_Str + "\" + dirInfo_Str);
                            directory.Create();
                        }
                        getDirs(localPath_Str + "\" + dirInfo_Str, dirInfo[i]);
    
                    }
                }
            }
    下载文件
            /// <summary>
            /// 将目标文件读成流,写入到目标文件中
            /// </summary>
            /// <param name="serverPath">源文件</param>
            /// <param name="localPath">要写入的文件</param>
            public void DownLoadFils(string serverPath, string localPath)
            {
                StreamReader reader = new StreamReader(serverPath);//初始化读取
                StreamWriter writer = new StreamWriter(localPath);//初始化写入
                string readAll = reader.ReadToEnd();//从流中读取全部
                //将流写到到所创建的文件中去。
                writer.Write(readAll);
                writer.Flush();
                reader.Close();
                writer.Close();        
            }
    下载文件2(使用流)
            public static void DownLoadFiles(string serverPath,string localPath)
            {
                if ((File.Exists(serverPath)) && (File.Exists(localPath)))
                {
                    FileStream fSource = File.Open(serverPath, FileMode.Open); //初始化文件流
                    byte[] bty_Ary = new byte[fSource.Length];//初始化字节数组
                    fSource.Read(bty_Ary, 0, bty_Ary.Length);//读取流中数据把它写到字节数组中
                    fSource.Close();//关闭流                   
                    FileStream fTarget = File.Open(localPath, FileMode.Append);//初始化文件流
                    fTarget.Write(bty_Ary, 0, bty_Ary.Length);//将字节数组写入文件流
                    fTarget.Close();//关闭流
                }            
                
                
                //string str = Encoding.Default.GetString(bty_Ary);//将字节数组内容转化为字符串      
                //byte[] array = Encoding.UTF8.GetBytes("Hello World!你好");//给字节数组赋值            
    
            }
    下载文件(使用流2)
    var Info = System.Diagnostics.FileVersionInfo.GetVersionInfo(Path);
    获取文件相关信息

     

            public static bool CheckFolderExist(string path)
            {
                if (path.Trim().Length > 0)
                {
                    DirectoryInfo directory = new DirectoryInfo(path);
                    return directory.Exists;
                }
                else
                {
                    return false;
                }
            }
    检查文件夹是否存在
            public static void CreateDirectory(string path)
            {
                if (path.Trim().Length > 0)
                {
                    DirectoryInfo directory = new DirectoryInfo(path);
                    directory.Create();
                }
            }
    创建文件夹
                FileInfo f1 = new FileInfo(@"F:A_插件james.txt");
                f1.Create();
    创建文件
            public static bool ByteStreamToFile(string Path, byte[] StreamByte, bool isZip)
            {
                try
                {
                    byte[] bytes;
                    if (isZip == true)
                    {
                        bytes = GZip.Decompress(StreamByte);
                    }
                    else
                    {
                        bytes = StreamByte;
                    }
    
    
                    //创建文件流(创建的方式,文件存在则覆盖)
                    FileStream fileStream = new FileStream(Path, FileMode.Create);
                    //把文件流写到文件中
                    fileStream.Write(bytes, 0, bytes.Length);
                    fileStream.Flush();
                    fileStream.Close();
                    fileStream.Dispose();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
    把二进制数组读成文件
            public static void CopyFile(string sourceFileName, string destFileName)
            {
                File.Copy(sourceFileName, destFileName, true);
            }
    简单的拷贝文件
            public static void DeleteFile(string filePath)
            {
                try
                {
                    //c# 不能删除只读文件
                    File.SetAttributes(filePath, FileAttributes.Normal);
                    if (File.Exists(filePath))
                    {
                        File.Delete(filePath);
                    }
                }
                catch (Exception ex)
                {
                    //  MessageBox.Show(ex.ToString());
                    throw ex;
                }
    
            }
    删除文件
    string[] Info_Ary = Directory.GetFileSystemEntries(@"F:A_插件");
    返回文件夹内的 文件 + 文件夹

     

         string path = context.Server.MapPath(@"~/CISWeb/SMT_SOP");
            DirectoryInfo theFolder = new DirectoryInfo(path);
            DirectoryInfo[] dirInfo = theFolder.GetDirectories();
            //遍历文件夹
            string strName = "";
            foreach (DirectoryInfo NextFolder in dirInfo)
            {
                // this.listBox1.Items.Add(NextFolder.Name);
                FileInfo[] fileInfo = NextFolder.GetFiles();
                foreach (FileInfo NextFile in fileInfo)  //遍历文件
                    strName = NextFile.Name;
            }
    遍历指定文件夹下的文件
            public void UseApp() { 
                system.Diagnostics.Process.Start("C:SayHi.exe","para1 para2");
            }
    
    // para1 和para 2是调用的时候,传入的两个参数
    调用应用程序
  • 相关阅读:
    TypeScript总结
    echarts超全超详情配置项
    《JavaScript高级程序设计》笔记
    ES6`…`扩展(spread)/收集(rest)运算符详解
    JavaScript深拷贝浅拷贝全析
    使用lodash.cloneDeep实现深拷贝
    MessageChannel
    JavaScript相等操作符(==)
    微信昵称emoji表情,特殊表情导致列表不显示,导出EXCEL报错等问题解决!
    SQL某个字段在原内容上增加固定内容或replace查找替换内容
  • 原文地址:https://www.cnblogs.com/vichin/p/5731465.html
Copyright © 2011-2022 走看看