做了一個android與unity互動的,手機保存了一張圖片到電腦上,怎么判斷檔案中有圖片更新。
uj5u.com熱心網友回復:
首先你要明確的是你要干嘛?監聽檔案還是檔案夾?監聽的是電腦的還是Android的?
看你的意思應該是監聽電腦檔案夾是否跟新了你從手機傳過去的那張圖片?
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace FileWatchered
{
class Program
{
static void Main(string[] args)
{
WatcherStrat(@"檔案夾路徑", "string"); //*.*表示監聽檔案夾下所有檔案,“.txt”監視某種格式的檔案
Console.ReadKey();
}
private static void WatcherStrat(string path, string filter)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = path;
watcher.Filter = filter;
watcher.Changed += new FileSystemEventHandler(OnProcess);
watcher.Created += new FileSystemEventHandler(OnProcess);
watcher.Deleted += new FileSystemEventHandler(OnProcess);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
watcher.EnableRaisingEvents = true;
watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess
| NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;
watcher.IncludeSubdirectories = true;
}
private static void OnProcess(object source, FileSystemEventArgs e)
{
if (e.ChangeType == WatcherChangeTypes.Created)
{
OnCreated(source, e);
}
else if (e.ChangeType == WatcherChangeTypes.Changed)
{
OnChanged(source, e);
}
else if (e.ChangeType == WatcherChangeTypes.Deleted)
{
OnDeleted(source, e);
}
}
private static void OnCreated(object source, FileSystemEventArgs e)
{
Console.WriteLine("檔案新建事件處理邏輯 {0} {1} {2}", e.ChangeType, e.FullPath, e.Name);
}
private static void OnChanged(object source, FileSystemEventArgs e)
{
Console.WriteLine("檔案改變事件處理邏輯{0} {1} {2}", e.ChangeType, e.FullPath, e.Name);
}
private static void OnDeleted(object source, FileSystemEventArgs e)
{
Console.WriteLine("檔案洗掉事件處理邏輯{0} {1} {2}", e.ChangeType, e.FullPath, e.Name);
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
Console.WriteLine("檔案重命名事件處理邏輯{0} {1} {2}", e.ChangeType, e.FullPath, e.Name);
}
}
}
這個是windows監聽檔案或者檔案夾的,你自己改改。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/67559.html
標籤:Unity3D
