Click here to Skip to main content
15,507,912 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
Hello, i am working on a program which should include an File-Explorer system. So I wanted to program the Explorer seperatly from the main project (for now) and planned to implement it, as soon as it works (i hop i can manage it somehow)

Visual Studio (2019) gives an error (CS1061)
'IDisposable' does not contain a definition for 'Handle' and no accessible extension method 'Handle' accepting a first argument of type 'IDisposable' could be found (are you missing a using directive or an assembly reference?).


The error occurs at line 188 (use ctrl + f and copy - paste: "
System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(icon.Handle,
" to find Line 188 in code)

So my Code is the following

C#
  1  using System;
  2  using System.Collections.Generic;
  3  using System.Collections.ObjectModel;
  4  using System.Drawing;
  5  using System.IO;
  6  using System.Linq;
  7  using System.Runtime.InteropServices;
  8  using System.Text;
  9  using System.Windows.Media;
 10  using System.Windows.Media.Imaging;
 11  using static FileExplorer.MainWindow;
 12  
 13  namespace FileExplorer
 14  {
 15      class FileSystem
 16      {
 17      }
 18  
 19      public class FileSystemObjectInfo : BaseObject
 20      {
 21          public FileSystemObjectInfo(DriveInfo drive)
 22              : this(drive.RootDirectory)
 23          {
 24          }
 25  
 26          public FileSystemObjectInfo(FileSystemInfo info)
 27          {
 28          }
 29  
 30          internal class DummyFileSystemObjectInfo : FileSystemObjectInfo
 31          {
 32              public DummyFileSystemObjectInfo()
 33                  : base(new DirectoryInfo("DummyFileSystemObjectInfo"))
 34              {
 35              }
 36          }
 37  
 38          public event EventHandler BeforeExpand;
 39  
 40          public event EventHandler AfterExpand;
 41  
 42          public event EventHandler BeforeExplore;
 43  
 44          public event EventHandler AfterExplore;
 45  
 46          private void RaiseBeforeExpand()
 47          {
 48              BeforeExpand?.Invoke(this, EventArgs.Empty);
 49          }
 50  
 51          private void RaiseAfterExpand()
 52          {
 53              AfterExpand?.Invoke(this, EventArgs.Empty);
 54          }
 55  
 56          private void RaiseBeforeExplore()
 57          {
 58              BeforeExplore?.Invoke(this, EventArgs.Empty);
 59          }
 60  
 61          private void RaiseAfterExplore()
 62          {
 63              AfterExplore?.Invoke(this, EventArgs.Empty);
 64          }
 65  
 66  
 67          public ObservableCollection<FileSystemObjectInfo> Children
 68          {
 69              get { return base.GetValue<ObservableCollection<FileSystemObjectInfo>>("Children"); }
 70              private set { base.SetValue("Children", value); }
 71          }
 72  
 73          public ImageSource ImageSource
 74          {
 75              get { return base.GetValue<ImageSource>("ImageSource"); }
 76              private set { base.SetValue("ImageSource", value); }
 77          }
 78  
 79          public bool IsExpanded
 80          {
 81              get { return base.GetValue<bool>("IsExpanded"); }
 82              set { base.SetValue("IsExpanded", value); }
 83          }
 84  
 85          public FileSystemInfo FileSystemInfo
 86          {
 87              get { return base.GetValue<FileSystemInfo>("FileSystemInfo"); }
 88              private set { base.SetValue("FileSystemInfo", value); }
 89          }
 90  
 91          private DriveInfo Drive
 92          {
 93              get { return base.GetValue<DriveInfo>("Drive"); }
 94              set { base.SetValue("Drive", value); }
 95          }
 96  
 97  
 98          private void AddDummy()
 99          {
100              this.Children.Add(new DummyFileSystemObjectInfo());
101          }
102  
103          private bool HasDummy()
104          {
105              return !object.ReferenceEquals(this.GetDummy(), null);
106          }
107  
108          private DummyFileSystemObjectInfo GetDummy()
109          {
110              var list = this.Children.OfType<DummyFileSystemObjectInfo>().ToList();
111              if (list.Count > 0) return list.First();
112              return null;
113          }
114  
115          private void RemoveDummy()
116          {
117              this.Children.Remove(this.GetDummy());
118          }
119  
120  
121          private void ExploreDirectories()
122          {
123              if (Drive?.IsReady == false)
124              {
125                  return;
126              }
127              if (FileSystemInfo is DirectoryInfo)
128              {
129                  var directories = ((DirectoryInfo)FileSystemInfo).GetDirectories();
130                  foreach (var directory in directories.OrderBy(d => d.Name))
131                  {
132                      if ((directory.Attributes & FileAttributes.System) != FileAttributes.System &&
133                          (directory.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
134                      {
135                          var fileSystemObject = new FileSystemObjectInfo(directory);
136                          fileSystemObject.BeforeExplore += FileSystemObject_BeforeExplore;
137                          fileSystemObject.AfterExplore += FileSystemObject_AfterExplore;
138                          Children.Add(fileSystemObject);
139                      }
140                  }
141              }
142          }
143  
144          private void FileSystemObject_AfterExplore(object sender, EventArgs e)
145          {
146              RaiseAfterExplore();
147          }
148  
149          private void FileSystemObject_BeforeExplore(object sender, EventArgs e)
150          {
151              RaiseBeforeExplore();
152          }
153  
154  
155          private void ExploreFiles()
156          {
157              if (Drive?.IsReady == false)
158              {
159                  return;
160              }
161              if (FileSystemInfo is DirectoryInfo)
162              {
163                  var files = ((DirectoryInfo)FileSystemInfo).GetFiles();
164                  foreach (var file in files.OrderBy(d => d.Name))
165                  {
166                      if ((file.Attributes & FileAttributes.System) != FileAttributes.System &&
167                          (file.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
168                      {
169                          Children.Add(new FileSystemObjectInfo(file));
170                      }
171                  }
172              }
173          }
174  
175  
176  
177          public static class FolderManager
178          {
179              public static ImageSource GetImageSource(string directory, ItemState folderType)
180              {
181                  return GetImageSource(directory, new Size(16, 16), folderType);
182              }
183  
184              public static ImageSource GetImageSource(string directory, Size size, ItemState folderType)
185              {
186                  using (var icon = ShellManager.GetIcon(directory, ItemType.Folder, IconSize.Large, folderType))
187                  {
188                   Return System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(icon.Handle,
189                          System.Windows.Int32Rect.Empty,
190                          BitmapSizeOptions.FromWidthAndHeight(size.Width, size.Height));
191                  }
192              }
193          }
194  
195  
196          /// <summary>
197          /// Hier weitermachen stand, siehe Screenshots --> Seite ist auf Stick Log gespeichert
198          /// 
199          /// https://medium.com/explorer-565a3f13f6f2
200          /// 
201          /// </summary>
202  
203  
204  
205          class FileSystem : IDisposable
206          {
207              // Flag: Has Dispose already been called?
208              bool disposed = false;
209              // Instantiate a FileStream instance.
210              FileStream fs = new FileStream("test.txt", FileMode.OpenOrCreate);
211  
212              // Public implementation of Dispose pattern callable by consumers.
213              public void Dispose()
214              {
215                  Dispose(disposing: true);
216                  GC.SuppressFinalize(this);
217              }
218  
219              // Protected implementation of Dispose pattern.
220              protected virtual void Dispose(bool disposing)
221              {
222                  if (disposed)
223                      return;
224  
225                  if (disposing)
226                  {
227                      fs.Dispose();
228                      // Free any other managed objects here.
229                      //
230                    
231                  }
232  
233                  disposed = true;
234              }
235          }
236  
237  
238  
239              public class ShellManager : ShellManagerBase
240          {
241              internal static IDisposable GetIcon(string directory, ItemType folder, IconSize large, ItemState folderType)
242              {
243                  throw new NotImplementedException();
244              }
245          }
246  
247      }
248  
249      internal class Icon
250      {
251          internal static object FromHandle(object hIcon)
252          {
253              throw new NotImplementedException();
254          }
255      }
256  }


What I have tried:

I've tried a lot of things but just can't fix the error

I really need your help :(

Thanks i.a.
Posted
Updated 21-Sep-22 22:44pm
v3

1 solution

Your ShellManager.GetIcon method is returning the wrong type - it's returning IDisposable, which, as the error correctly tells you, doesn't contain a Handle property.

You need to fix that method to return the correct type. Since you haven't provided any implementation of that method, nor any type that it could feasibly return, we can't tell you how to do that.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900