「Xamarin.Macでリムーバブルメディアの着脱を検知する」の版間の差分

imported>Administrator
編集の要約なし
imported>Administrator
 
(同じ利用者による、間の3版が非表示)
1行目: 1行目:
Xamarin.Macで[[CD-ROM]]や[[DVD-ROM]]、[[USBメモリ]]の挿入などを検出できると何かと嬉しい。
[[Xamarin.Mac]]で[[CD-ROM]]や[[DVD-ROM]]、[[USBメモリ]]の挿入などを検出できると何かと嬉しい。


== 実装例 ==
==実装例==
NSWorkspace.SharedWorkspace.NotificationCenterを使うと検出できるようだ。この通知で情報として飛んでくるのは変化のあったデバイスのパスのみなので、そのパスをNSWorkspace.SharedWorkspace.GetFileSystemInfoを使い[[CD-ROM]]なのか[[USBメモリ]]なのかなどを判別する。なお、CD-ROMやDVD-ROMの場合はディスクフォーマットごとに返ってくる値が違うようだ。どんな種類があるのか全部はまだ把握していない。
NSWorkspace.SharedWorkspace.NotificationCenterを使うと検出できるようだ。この通知で飛んでくるのは「変化のあったデバイスのパスのみ」なので、そのパスをNSWorkspace.SharedWorkspace.GetFileSystemInfoに投げて「[[CD-ROM]]なのか」「[[USBメモリ]]なのか」などを判別する。なお、GetFileSystemInfoの結果は[[CD-ROM]]や[[DVD-ROM]]の場合はディスクフォーマットごとに返ってくる値が違うようだ。どんな種類があるのか全部はまだ把握していない。


通知には種類があるようで、この例では以下を実装している。
通知には種類があるようで、この例では以下を実装している。
* NSWorkspaceDidMountNotification - マウント直後
 
* NSWorkspaceWillUnmountNotification - アンマウント直前
*NSWorkspaceDidMountNotification - マウント直後
*NSWorkspaceWillUnmountNotification - アンマウント直前
 
NSWorkspaceDidUnmountNotification(WillではなくDid)も実装すればアンマウント直前のみならずアンマウント直後も検出できる。ただしアンマウント後なのでデバイスパスしか取得できない。アンマウント失敗時を考慮するとこちらの方が確実かもしれないが詳しくは知らない。
NSWorkspaceDidUnmountNotification(WillではなくDid)も実装すればアンマウント直前のみならずアンマウント直後も検出できる。ただしアンマウント後なのでデバイスパスしか取得できない。アンマウント失敗時を考慮するとこちらの方が確実かもしれないが詳しくは知らない。


<source lang="csharp">
<source lang="csharp">
     using System;
     using System;
     using MonoMac.Foundation;
     using Foundation;
     using MonoMac.AppKit;
     using AppKit;


     public class RemovableMediaNotificationEventArgs : EventArgs
     public class RemovableMediaNotificationEventArgs : EventArgs
52行目: 54行目:
             if (_mountObserver == null)
             if (_mountObserver == null)
             {
             {
                 _mountObserver = notification.AddObserver("NSWorkspaceDidMountNotification", DidMount);
 
                 _mountObserver = notification.AddObserver(NSWorkspace.DidMountNotification, DidMount);
             }
             }


58行目: 61行目:
             if (_unmountObserver == null)
             if (_unmountObserver == null)
             {
             {
                 _unmountObserver = notification.AddObserver("NSWorkspaceWillUnmountNotification", WiilUnmount);
 
                 _unmountObserver = notification.AddObserver(NSWorkspace.WillUnmountNotification, WiilUnmount);
             }
             }
         }
         }
111行目: 115行目:


             var workspace = NSWorkspace.SharedWorkspace;
             var workspace = NSWorkspace.SharedWorkspace;
             workspace.GetFileSystemInfo(devicePath, out isRemovable, out isWritable,out isUnmountable, out description, out fileSystemType);
             workspace.GetFileSystemInfo(devicePath, out isRemovable, out isWritable, out isUnmountable, out description, out fileSystemType);


             var args = new RemovableMediaNotificationEventArgs {
             var args = new RemovableMediaNotificationEventArgs
            {
                 DevicePath = devicePath,
                 DevicePath = devicePath,
                 IsRemovable = isRemovable,
                 IsRemovable = isRemovable,
127行目: 132行目:


==関連項目==
==関連項目==
*[[Xamarin.Mac/CDやDVDなどの光学メディアを排出する]]
* [[Xamarin.MacでCDやDVDなどの光学メディアを排出する]]
 
* [[Xamarin.Macで接続されているリムーバブルメディアの一覧を取得する]]
==参考文献==
{{reflist}}
 
{{stub}}


[[category:MonoMac]]
[[category:MonoMac]]
[[category:Xamarin.Mac]]
[[category:Xamarin.Mac]]