FileManager 的基本操作

    var domainListPath: String?

    var signaturePath: String?

    var publicKeyPath: String?

1、从documentDirectory获取文件 

        let sandBoxPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).map(\.path)

        domainListPath = URL(fileURLWithPath: sandBoxPath[0]).appendingPathComponent("1.list").path

        signaturePath = URL(fileURLWithPath: sandBoxPath[0]).appendingPathComponent("2").path

        publicKeyPath = URL(fileURLWithPath: sandBoxPath[0]).appendingPathComponent("3.key").path


        2、如果没有就从bundle把文件复制到documents里面

        letfileMgr =FileManager.default

        fileMgr.delegate=self

        if(!fileMgr.fileExists(atPath:domainListPath!) ||

            !fileMgr.fileExists(atPath:signaturePath!) ||

            !fileMgr.fileExists(atPath:publicKeyPath!)) {

            do{

                tryfileMgr.copyItem(

                    atPath: URL(fileURLWithPath: Bundle.main.resourcePath ?? "").appendingPathComponent("/default_list/domainip.list").path,

                    toPath:domainListPath!)

                tryfileMgr.copyItem(

                    atPath: URL(fileURLWithPath: Bundle.main.resourcePath ?? "").appendingPathComponent("/default_list/signature").path,

                    toPath:signaturePath!)

                tryfileMgr.copyItem(

                    atPath: URL(fileURLWithPath: Bundle.main.resourcePath ?? "").appendingPathComponent("/default_list/public.key").path,

                    toPath:publicKeyPath!)

            }catch{

            }

        }

//MARK  FileManagerDelegate methods

    funcfileManager(_fileManager:FileManager,shouldCopyItemAtPathsrcPath:String,toPathdstPath:String) ->Bool{

        try? fileManager.removeItem(atPath: srcPath)

        if!FileManager.default.fileExists(atPath: srcPathasString) {

            do{

                tryFileManager.default.copyItem(atPath: srcPathasString,toPath: dstPathasString)


            }catch  {

                Log.Debug("func fileManager(_ fileManager: FileManager, shouldCopyItemAtPath srcPath: String, toPath dstPath: String) -> Bool")

            }

        }

        return true

    }


    //mark DCheckerManager delegate methods

    func overwriteConfigurations() {

        letfileMgr =FileManager.default

        fileMgr.delegate=self


        do{

            tryfileMgr.copyItem(

                atPath: URL(fileURLWithPath: Bundle.main.resourcePath ?? "").appendingPathComponent("/default_list/domainip.list").path,

                toPath:domainListPath!)

            tryfileMgr.copyItem(

                atPath: URL(fileURLWithPath: Bundle.main.resourcePath ?? "").appendingPathComponent("/default_list/signature").path,

                toPath:signaturePath!)

            tryfileMgr.copyItem(

                atPath: URL(fileURLWithPath: Bundle.main.resourcePath ?? "").appendingPathComponent("/default_list/public.key").path,

                toPath:publicKeyPath!)

        }catch{

        }

    }

欢迎大家加入QQ群一起交流学习:

464406500

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容