Advertisement
AdamJS

simply_demo_refresh_desktop

Dec 18th, 2022 (edited)
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.11 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. using System.Windows.Forms;
  6.  
  7. namespace WindowsFormsApplication1
  8. {
  9.     public partial class Form1 : Form
  10.     {
  11.         private int last_file_count = 0;
  12.         private string path_to_current_user_dektop = "";
  13.  
  14.         [DllImport("shell32.dll", CharSet = CharSet.Auto)]
  15.         static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);
  16.         const uint SHCNE_ASSOCCHANGED = 0x08000000;
  17.         const uint SHCNF_IDLIST = 0x0000;
  18.        
  19.         public Form1()
  20.         {
  21.             // dodać do Form - Label i Timer
  22.  
  23.             InitializeComponent();
  24.             timer1.Interval = 1000; // 1000ms = 1s.
  25.  
  26.             // ustawienie Form w prawym górnym rogu ekranu
  27.             // docelowo napisać kod, który pokaże tylko ikone w "zasobniku"
  28.             int screenWidth = Screen.PrimaryScreen.Bounds.Width;
  29.             this.StartPosition = FormStartPosition.Manual;
  30.             this.Location = new Point((screenWidth - this.Width), 0);
  31.  
  32.             // pobranie ścieżki do pulpitu zalogowanego użytkownika
  33.             path_to_current_user_dektop = Environment.ExpandEnvironmentVariables(@"%userprofile%\Desktop");
  34.             // zainicjowanie ilości plików na pulpicie
  35.             last_file_count = fileCount();
  36.  
  37.             // właczenie timera, ktory co 1s sprawdzi zawartość pulpitu
  38.             timer1.Enabled = true;
  39.         }
  40.  
  41.         private void timer1_Tick(object sender, EventArgs e)
  42.         {
  43.             int current_file_count = fileCount();
  44.             label1.Text = "Number of files on desktop: " + current_file_count;
  45.             if (current_file_count != last_file_count)
  46.             {
  47.                 last_file_count = current_file_count;
  48.                 // odśwież zawartośc pulpitu użytkownika
  49.                 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, IntPtr.Zero, IntPtr.Zero);
  50.             }
  51.         }
  52.  
  53.         private int fileCount()
  54.         {
  55.             return Directory.GetFiles(path_to_current_user_dektop).Length;
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement