Windows task snippets

Snippets of ready-to-use code that accomplish small but useful tasks of interest to Universal Windows Platform (UWP) app developers. These snippets represent simple solutions to common problems, and simple recipes to help you implement new app features.

Every snippet includes the using statements that it requires, which you can add to the top of your file as needed.

Each snippet is written in C# unless otherwise indicated. Click the GitHub link to go directly to the source code, and the doc link for some background.

Files and folders

Sample Description Code Sample Related docs
File exists Determines whether a file exists in a given folder or a folder subtree. The file name should include the extension, but is not case-sensitive. GitHub StorageFolder Class,StorageFile class
Preserve access to a StorageFolder Saves your app's access permissions to the specific StorageFolder to which a user has granted access. GitHub Files, folders, and libraries,StorageApplicationPermissions Class
Launch executable Launches the default app or other executable (.exe or .bat file) associated with the specified filename extension. GitHub Launcher class,launcher.LaunchFileAsync method

Colors

Sample Description Code Sample Related docs
Colors as a collection Gets a collection containing all system colors supported in Universal Windows Platform (UWP) apps. GitHub Colors class,Color structure
Convert hex to color Converts a hexadecimal string representation of a color to its Windows.UI.Color ARGB (alpha, red, green, blue) equivalent or vice-versa. GitHub ARGB
Convert RGB to HSV Converts RGB (red, green, blue) color values to HSV (hue, saturation, value). Since Universal Windows Platform (UWP) system colors (contained in the Windows.UI.Colors class) only provide access to RGB values, this utility is useful for converting them if needed. GitHub Colors class

UI Threading

Sample Description Code Sample Related docs
UI thread access from background thread Enables code running on a background thread to schedule work that will run on the UI thread. GitHub CoreDispatcher class
UI thread task await from background thread Enables code running on a background thread to await a task that must run on the UI thread. GitHub CoreDispatcher class,TaskCompletionSource class
UI updates with a timer Performs the specified action at the specified interval in minutes. GitHub DispatcherTimer class

Sockets

Sample Description Code Sample Related docs
Socket programming with TCP These methods provide simple string-based communication over a TCP socket connection. GitHub StreamSocket class,Exceptions in Windows.Networking.Sockets
Socket programming with UDP These methods provide simple string-based communication over a UDP socket connection. GitHub DatagramSocket class,Exceptions in Windows.Networking.Sockets

Popups

Sample Description Code Sample Related docs
Show pop-up asking user to rate app Displays a pop-up message that asks the user to rate the app. If the user agrees, they are redirected to the app's store page to indicate their rating. GitHub MessageDialog class,UICommand class
Show dialog box These methods provide convenient ways to show a dialog box, and can be called from the UI thread or from a background thread. GitHub MessageDialog class,UICommand class

Miscellaneous

Sample Description Code Sample Related docs
App version and title Gets the title and version of an app as specified in the project manifest. GitHub Package class
Bing image-of-the-day URI Gets the URI for the the Bing image of the day, optionally for the specified resolution and market. GitHub HttpClient class, Uri class
Clipboard text Gets or sets the current clipboard text. GitHub Clipboard class
Convert the DPI of rectangles (C++) Converts a rectangle in a window from logical coordinates to physical coordinates or vice-versa, regardless of the dots per inch (DPI) awareness of the caller. GitHub PhysicalToLogicalPointForPerMonitorDPI function,LogicalToPhysicalPointForPerMonitorDPI function
Data binding change notification Provides a simple, standard change-notification implementation. GitHub Data binding, INotifyPropertyChanged interface
Play sound once or looped Plays a sound, either once or continuously. The sound is passed in as an IStorageFile. You can load your sound file as a StorageFile object by using a method like GetFileFromApplicationUriAsync. GitHub IStorageFile interface, MediaElement class, Audiograph class

See also