• F#小程序——上传指定类型文件到云服务器或下载到本地


    当时根据涛哥的要求,做了一个小工具,具体作用就是上传或下载指定类型文件到云服务器。当时涛哥正在做VS的一个插件: F# snippet ,类似于C#中的.net AutoCode v4.0,区别就在于它们工作的语言不同。

    这个工具使用到了F#3.0的新功能:Type Provider and Query.如果你想尝试一下这个工具(当然,你需要做些修改:)),那么你需要F# 3.0 或者 VS2012。如果只想了解一下这个工具,也不错~ 呵呵

    下面是代码:)

    // Learn more about F# at http://fsharp.net
    // See the 'F# Tutorial' project for more help.
    open Microsoft.FSharp.Data.TypeProviders
    open System
    open System.Collections
    //input your connection string of you DB on cloud
    type internal T = Microsoft.FSharp.Data.TypeProviders.SqlEntityConnection<"????">
    let internal data = T.GetDataContext().SnippetTable
    
    let UpLoadToDB(fileName : string, fileContent) = 
        let newData = new T.ServiceTypes.SnippetTable()  
        let guid = System.Guid.NewGuid()
        let time = System.DateTime.Now
        newData.FileName <- fileName
        newData.SnippetContent <- fileContent
        newData.CreatedDateTime <- Nullable.op_Implicit(time)
        newData.Language <- 0
        newData.IsEnable <- true
        newData.ID <- guid
        newData.Language <- 0
        let dataContent = T.GetDataContext()
        
        dataContent.SnippetTable.AddObject(newData)
        dataContent.DataContext.SaveChanges() |> ignore
    
    let UpLoadFileByDic(dic : string) =
        if (System.IO.File.Exists(dic) = true || System.IO.Directory.Exists(dic) = false) then
            printfn "Please upload a folder"
        else
            let filesHolder = System.IO.Directory.GetFiles(dic)
            let fileSeq = filesHolder |> Array.toSeq
            let snippetFileSeq = fileSeq |> Seq.filter (fun file -> System.IO.Path.GetExtension(file) = ".snippet") 
    
            snippetFileSeq
                |> Seq.iter( fun filepath ->
                                let filePath = System.IO.Path.Combine(dic, filepath)                            
                                let name = System.IO.Path.GetFileNameWithoutExtension(filepath)
                                use streamReader = new System.IO.StreamReader(filePath)
                                let content = streamReader.ReadToEnd()
                                UpLoadToDB(name,content)
                                
                                )
    
    let CreatFileForBackUp(fileName : string, fileContext : string) =
        let currentFolder = System.Environment.CurrentDirectory
        let newFolder = System.IO.Path.Combine(currentFolder, "backup")
        System.IO.Directory.CreateDirectory(newFolder) |> ignore
        let fileNameAndExtension = fileName + ".snippet"
        let fullFilePath = System.IO.Path.Combine(newFolder,fileNameAndExtension)
        if(System.IO.File.Exists(fullFilePath) <> true) then
            use streamWriter = System.IO.File.CreateText(fullFilePath)
            streamWriter.WriteLine(fileContext)
            streamWriter.Close()
        
    let BackUp() = 
        let item = 
            query{
                for i in data do
                where(i.FileName <> "")
                select i
            }
        
        item 
            |> Seq.iter( fun i -> CreatFileForBackUp( i.FileName,i.SnippetContent))
        printfn "Done, anything else you want to do?"
    
    
    let WaitUserInputDic() =
        printfn "Please input the folder path"
        let path = Console.ReadLine().Trim()
        UpLoadFileByDic(path)
        printfn "Done, anything else you want to do?"
    let Execute() =
        Console.ForegroundColor <- ConsoleColor.Red
        Console.BackgroundColor <- ConsoleColor.DarkGreen
        Console.Clear()   
        printfn "Please select the command you want to execute(input the number of the command):"
        printfn ""
        printfn " 1 : Back Up(DownLoad all snippets in DataBase to the folder backup which is in current folder)"
        printfn ""
        printfn " 2 : Up load a folder to DataBase(upload all snippet files in the folder,not include those in subfolder)"
        printfn ""
        printfn " 3 : Exist"
        let rec selectCommand() = 
            let command = Console.ReadLine()
            let tripCommand = command.Trim()
            match tripCommand with
            | "1" ->  BackUp();selectCommand()
            | "2" ->  WaitUserInputDic();selectCommand()
            | "3" -> ()
            | _ -> printfn "Please the command number"; selectCommand()
        selectCommand()
    
    [<EntryPoint>]
    let main argv = 
        Execute()
        0


    代码中的函数 UpLoadFileByDic() 负责上传至DB, BackUp()负责下载DB上以存的Snippet到当前目录。UpLoadToDB 函数则是对DB中的表进行操作,生成并插入一个新表。

    注意:在这行代码 type internal T = Microsoft.FSharp.Data.TypeProviders.SqlEntityConnection  后面加的参数是连接DB的那个Connection string,有个技巧就是:你可以从C#中连接到这个数据库,然后从属性里面可以找到这个Connection string。

    Ok,代码就这么多。。。留个笔记也好~:)

  • 相关阅读:
    烟花散尽漫说无(參考资料)
    [Leetcode]-Majority Element
    百度移动搜索測试电面
    可删除超炫&amp;多种特效的Card视图(改造自cardsui-for-android开源项目),提供DEMO下载
    关闭 You need to use a Theme.AppCompat theme (or descendant) with this activity解决方法
    Android Snackbar使用方法及小技巧-design
    Android Intent Action 大全
    android权限大全
    Android 判断SD卡是否存在及容量查询
    Android中蓝牙的基本使用----BluetoothAdapter类简介
  • 原文地址:https://www.cnblogs.com/FsharpZack/p/2765904.html
Copyright © 2020-2023  润新知