複製資源文件(/Resources/Raw/)-文件處理

c

/// <summary>
/// 複製資源文件(/Resources/Raw/)
/// </summary>
/// <param name="resourceFileName">資源文件名</param>
public async static Task CopyFileFromResource(string resourceFileName)
{
    //FileSystem.Current.AppDataDirectory 程序文件目錄
    string filePath = System.IO.Path.Combine(FileSystem.Current.AppDataDirectory, resourceFileName);
    if (File.Exists(filePath))
    {
        File.Delete(filePath);
    }
    //資源文件是否存在
    bool resExists = await FileSystem.Current.AppPackageFileExistsAsync(resourceFileName);
    if (resExists)
    {
        //資源文件流
        using System.IO.Stream fileStream = await FileSystem.Current.OpenAppPackageFileAsync(resourceFileName);
        // 緩衝區爲10k
        byte[] buffer = new Byte[10000];
        // 文件長度
        int length;
        //目標文件流
        using System.IO.FileStream fs = new System.IO.FileStream(filePath, FileMode.OpenOrCreate);
        //循環寫入
        do
        {
            length = fileStream.Read(buffer, 0, 10000);
            fs.Write(buffer, 0, length);
            buffer = new Byte[10000];
        } while (length > 0);
        //刷新緩存,結束。
        fs.Flush();
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章