自己會做的有兩種,很簡單直接的File.Copy、和有點麻煩的FileStream方式。
首先來看File.Copy的方式,一行就輕鬆愉快地搞定;可是如果檔案很大時,那整個程式就會卡在那邊,畫面看起來就會像當機狀態,所以只適用於小型檔案和在本機端時:
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class XXX: MonoBehaviour {
void Start()
{
if(File.Exists("D:/aaa.mp4"))
{
File.Copy("D:/aaa.mp4", "D:/bbb.mp4");
}
}
void Update()
{
}
}
然後是FileStream的方式,雖然撰寫比較麻煩,但由於是另外執行,所以不會影響到程式裡其他部分的執行,是比較好的圓融方式,而實際上的複製速度會比File.Copy慢,適用於大型檔案和非本機端時:
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class XXX: MonoBehaviour {
void Start()
{
if(File.Exists("D:/aaa.mp4"))
{
StartCoroutine(DownloadMovie());
}
}
void Update()
{
}
IEnumerator DownloadMovie()
{
FileStream fromFileStream = null;
FileStream toFileStream = null;
byte[] buffer = new byte[32768];
int read;
fromFileStream = new FileStream("D:/aaa.mp4", FileMode.Open);
toFileStream = new FileStream("D:/bbb.mp4", FileMode.Create);
while ((read = fromFileStream.Read(buffer, 0, buffer.Length)) > 0)
{
toFileStream.Write(buffer, 0, read);
yield return new WaitForSeconds(0.01f);
}
fromFileStream.Close();
toFileStream.Close();
}
}
其他還有很多方式,這裡只是笨笨的我所摸過後會用的兩種。
沒有留言:
張貼留言