File tree Expand file tree Collapse file tree 2 files changed +18
-9
lines changed
An2WinFileTransfer/An2WinFileTransfer Expand file tree Collapse file tree 2 files changed +18
-9
lines changed Original file line number Diff line number Diff line change @@ -7,28 +7,40 @@ namespace An2WinFileTransfer.Services
77{
88 public class DeviceService
99 {
10+ private readonly IEnumerable < MediaDevice > _mediaDevices ;
11+
12+ public DeviceService ( )
13+ {
14+ _mediaDevices = MediaDevice . GetDevices ( ) ;
15+ }
16+
1017 public IEnumerable < string > GetConnectedDeviceNames ( )
1118 {
12- return MediaDevice . GetDevices ( ) . Select ( d => d . FriendlyName ) ;
19+ return _mediaDevices . Select ( d => d . FriendlyName ) ;
1320 }
1421
1522 public MediaDevice ConnectToDevice ( string deviceName )
1623 {
17- var device = MediaDevice . GetDevices ( )
18- . FirstOrDefault ( d => d . FriendlyName == deviceName ) ;
24+ var device = _mediaDevices . FirstOrDefault ( d => d . FriendlyName == deviceName ) ;
1925
2026 if ( device == null )
2127 {
2228 throw new InvalidOperationException ( $ "Device '{ deviceName } ' not found.") ;
2329 }
2430
25- device . Connect ( ) ;
31+ if ( ! device . IsConnected )
32+ {
33+ device . Connect ( ) ;
34+ }
35+
2636 return device ;
2737 }
2838
2939 public void DisconnectDevice ( MediaDevice device )
3040 {
31- if ( device ? . IsConnected == true )
41+ if ( device == null ) return ;
42+
43+ if ( device . IsConnected )
3244 {
3345 device . Disconnect ( ) ;
3446 }
Original file line number Diff line number Diff line change 77using An2WinFileTransfer . Extensions ;
88using An2WinFileTransfer . Models ;
99using An2WinFileTransfer . Services ;
10- using MediaDevices ;
1110
1211namespace An2WinFileTransfer . UI . Forms
1312{
@@ -169,16 +168,14 @@ private IEnumerable<FileType> LoadFileTypes()
169168
170169 private void PopulateDeviceList ( )
171170 {
172- _connectedDevices = MediaDevice . GetDevices ( ) . Select ( d => d . FriendlyName ) ;
171+ _connectedDevices = _deviceService . GetConnectedDeviceNames ( ) ;
173172
174- #if ! DEBUG
175173 if ( _connectedDevices . IsNullOrEmpty ( ) )
176174 {
177175 MessageBox . Show ( "No connected MTP devices found. Please connect your device and enable file transfer mode." ,
178176 "No Devices Found" , MessageBoxButtons . OK , MessageBoxIcon . Warning ) ;
179177 return ;
180178 }
181- #endif
182179
183180 comboBoxDeviceNames . Items . Clear ( ) ;
184181 comboBoxDeviceNames . Items . AddRange ( _connectedDevices . OrderBy ( n => n ) . ToArray ( ) ) ;
You can’t perform that action at this time.
0 commit comments