Posts

Showing posts from February, 2017

How to use resource files in your C# WPF project

Image
Some practical instructions on how to include resource files to your Visual Studio C# WPF project that are not source code. Such non-code files can include binary data, text files, media files, string values, icons, XML, or any other type of data. Project resource data is stored in XML format in the .resx file (named Resources.resx by default). In this example we demonstrate how resource files can be defined in and accessed from a separate class library, thereby helping to make your project cleaner and less cluttered. Step 1: Create a new Visual Studio WPF project. Create a new WPF application for this example: Step 2: Add a new class library project Right-click on your solution folder and select Add > New Project. Create a new Class Library application. We'll call it FileStore: This project we will use to contain our resource files. Step 3: Create a folder to store the resource files In the 'FileStore' Class Library project you created, right...

Using the WiX Toolset to create installers in Visual Studio C# projects

Image
Here is some of their online documentation on how to use the WiX Toolset to create installers: https://www.firegiant.com/wix/tutorial/getting-started Here is a quick and concise guide to set up using the WiX Toolset in a Visual Studio project, in order to create installation packages, from start to finish. Step 1: Install WiX Toolset First make sure you have installed the WiX toolset from the following site: http://wixtoolset.org/releases/ WiX Toolset when run looks like this: [caption id="attachment_7571" align="alignnone" width="355"] WiX Toolset to create installers[/caption] Step 2: Create a new Visual Studio project In this example I'm creating a new WPF application: Step 3: Add a WiX installer project to the solution Right click on your solution folder and select Add > New Project... Select 'Windows Installer XML' > 'Setup Project' and give your installer project a name: Step 4: generate GUID...

Listening for network availability changes in C#

Image
Firstly a console application program to consecutively ENABLE and DISABLE the network connectivity: [code language="csharp"] using System; using System.Diagnostics; namespace NetworkDisconnect { internal class Program { private static void Enable(string interfaceName) { var psi = new ProcessStartInfo("netsh", "interface set interface \"" + interfaceName + "\" enable"); var p = new Process {StartInfo = psi}; p.Start(); } private static void Disable(string interfaceName) { var psi = new ProcessStartInfo("netsh", "interface set interface \"" + interfaceName + "\" disable"); var p = new Process {StartInfo = psi}; p.Start(); } private static void Main(string[] args) { // Using 'netsh' Command, you can enable and disable “Local Area Connection...

Simple threading in C#

Image
Basic threading can quite easily be accomplished in C# by employing just a few lines of code. For this situation it is simply a matter of defining the function you wish to be run as a thread, starting that thread and using 'join' as a means of waiting for that thread to finish before continuing with the rest of your code. The following example show how to launch a function/task as a thread that operates independently of your main control loop: [code language="csharp"] using System; using System.Threading; namespace Threads { class Program { public static void Main(string[] args) { // Create and start the thread Thread thread = new Thread(new ThreadStart(SomeTaskThatNeedsToFinish)); thread.Start(); // Does not proceed beyond here until thread function has finished thread.Join(); // Once finished your program is free to continue... Console.WriteLine("Task completed. Pr...