コンテンツにスキップ
メインメニュー
メインメニュー
サイドバーに移動
非表示
案内
メインページ
最近の更新
未作成ページ
おまかせ表示
ヘルプ
MonoBook
検索
検索
ログイン
個人用ツール
ログイン
ログアウトした編集者のページ
もっと詳しく
投稿記録
トーク
「
Xamarin.Macでリムーバブルメディアの着脱を検知する
」を編集中
ページ
議論
日本語
閲覧
編集
ソースを編集
履歴表示
ツール
ツール
サイドバーに移動
非表示
操作
閲覧
編集
ソースを編集
履歴表示
全般
リンク元
関連ページの更新状況
特別ページ
ページ情報
警告:
ログインしていません。編集を行うと、あなたの IP アドレスが公開されます。
ログイン
または
アカウントを作成
すれば、あなたの編集はその利用者名とともに表示されるほか、その他の利点もあります。
スパム攻撃防止用のチェックです。 けっして、ここには、値の入力は
しない
でください!
[[Xamarin.Mac]]で[[CD-ROM]]や[[DVD-ROM]]、[[USBメモリ]]の挿入などを検出できると何かと嬉しい。 ==実装例== NSWorkspace.SharedWorkspace.NotificationCenterを使うと検出できるようだ。この通知で飛んでくるのは「変化のあったデバイスのパスのみ」なので、そのパスをNSWorkspace.SharedWorkspace.GetFileSystemInfoに投げて「[[CD-ROM]]なのか」「[[USBメモリ]]なのか」などを判別する。なお、GetFileSystemInfoの結果は[[CD-ROM]]や[[DVD-ROM]]の場合はディスクフォーマットごとに返ってくる値が違うようだ。どんな種類があるのか全部はまだ把握していない。 通知には種類があるようで、この例では以下を実装している。 *NSWorkspaceDidMountNotification - マウント直後 *NSWorkspaceWillUnmountNotification - アンマウント直前 NSWorkspaceDidUnmountNotification(WillではなくDid)も実装すればアンマウント直前のみならずアンマウント直後も検出できる。ただしアンマウント後なのでデバイスパスしか取得できない。アンマウント失敗時を考慮するとこちらの方が確実かもしれないが詳しくは知らない。 <source lang="csharp"> using System; using Foundation; using AppKit; public class RemovableMediaNotificationEventArgs : EventArgs { public string DevicePath { get; set; } public bool IsRemovable { get; set; } public bool IsWritable { get; set; } public bool IsUnmountable { get; set; } public string Description { get; set; } public string FileSystemType { get; set; } } public delegate void RemovableMediaNotificationEventHandler(object sender, RemovableMediaNotificationEventArgs args); // 着脱を検知するクラス public class RemovableMediaNotification : IDisposable { private NSObject _mountObserver; private NSObject _unmountObserver; public event RemovableMediaNotificationEventHandler OnMount; public event RemovableMediaNotificationEventHandler OnUnmount; public RemovableMediaNotification() { } public void Dispose() { Stop(); } public void Start() { var workspace = NSWorkspace.SharedWorkspace; var notification = workspace.NotificationCenter; // マウント直後 if (_mountObserver == null) { _mountObserver = notification.AddObserver(NSWorkspace.DidMountNotification, DidMount); } // マウント直前 if (_unmountObserver == null) { _unmountObserver = notification.AddObserver(NSWorkspace.WillUnmountNotification, WiilUnmount); } } public void Stop() { var workspace = NSWorkspace.SharedWorkspace; var notification = workspace.NotificationCenter; if (_mountObserver != null) { notification.RemoveObserver(_mountObserver); _mountObserver.Dispose(); _mountObserver = null; } if (_unmountObserver != null) { notification.RemoveObserver(_unmountObserver); _unmountObserver.Dispose(); _unmountObserver = null; } } private void DidMount(NSNotification notification) { if (OnUnmount != null) { var path = notification.UserInfo["NSDevicePath"].ToString(); var args = CreateEventArg(path); OnMount(this, args); } } private void WiilUnmount(NSNotification notification) { if (OnUnmount != null) { var path = notification.UserInfo["NSDevicePath"].ToString(); var args = CreateEventArg(path); OnUnmount(this, args); } } private RemovableMediaNotificationEventArgs CreateEventArg(string devicePath) { bool isRemovable; bool isWritable; bool isUnmountable; string description; string fileSystemType; var workspace = NSWorkspace.SharedWorkspace; workspace.GetFileSystemInfo(devicePath, out isRemovable, out isWritable, out isUnmountable, out description, out fileSystemType); var args = new RemovableMediaNotificationEventArgs { DevicePath = devicePath, IsRemovable = isRemovable, IsWritable = isWritable, IsUnmountable = isUnmountable, Description = description, FileSystemType = fileSystemType, }; return args; } } </source> ==関連項目== * [[Xamarin.MacでCDやDVDなどの光学メディアを排出する]] * [[Xamarin.Macで接続されているリムーバブルメディアの一覧を取得する]] [[category:MonoMac]] [[category:Xamarin.Mac]]
編集内容の要約:
MonoBookへの投稿はすべて、他の投稿者によって編集、変更、除去される場合があります。 自分が書いたものが他の人に容赦なく編集されるのを望まない場合は、ここに投稿しないでください。
また、投稿するのは、自分で書いたものか、パブリック ドメインまたはそれに類するフリーな資料からの複製であることを約束してください(詳細は
MonoBook:著作権
を参照)。
著作権保護されている作品は、許諾なしに投稿しないでください!
このページを編集するには、下記の確認用の質問に回答してください (
詳細
):
1たす1は?(全角で入力してください)
キャンセル
編集の仕方
(新しいウィンドウで開きます)
本文の横幅制限を有効化/無効化