Posts

Showing posts from September, 2016

How to rename a project folder in Visual Studio

Image
Example folder name ‘PortableMedia’ that I wish to rename to ‘Renishaw.IDT.Powers.PortableMedia’: Step 1: Close the VS solution and nename the folder in source control ‘PortableMedia’ Gets changed to ‘Renishaw.IDT.Powers.PortableMedia’: Re-open the solution. Observe that the renamed project that is now marked as unavailable in the Solution Explorer: Open the Properties pane on that unavailable folder: Just select properties window – you can’t right-click and select properties anymore… Edit the folder and update the path name: (If you have problems doing this then just edit the *.sln file using notepad++ and change the ‘File Path’ property in there) Right click on that unavailable project and try to reload project. This should work now: Remove the old folder in your workspace folder: You might get the following build whinge [code language="txt"] 1>------ Build started: Project: RenishawOnDemand.TestHarness, Configuration: Debug x64 -...

How to run processes and obtain the output in C#

Image
Example process: fsutil. Very useful link on obtaining and setting fsutil behaviour and properties at this following link: http://blogs.microsoft.co.il/skepper/2014/07/16/symbolic_link/ Typically contained in the System32 folder: C:\Windows\System32 It is quite simple to do and consists of two main steps: Step 1: Create Process object and set its StartInfo object accordingly [code language="csharp"] var process = new Process { StartInfo = new ProcessStartInfo { FileName = "C:\\Windows\\System32\\fsutil.exe", Arguments = "behavior query SymlinkEvaluation", UseShellExecute = false, RedirectStandardOutput = true, CreateNoWindow = true } }; [/code] Step 2: Start the process and read each line obtained from it: [code language="csharp"] process.Start(); while (!process.StandardOutput.EndOfStream) { var line = process.StandardOutput.ReadLine(); Console.WriteLine(line); } [/code] Full Code listing: [code language=...

Using Google Docs Viewer to embed online documents into your web page

Image
Some instructions on using Google Docs Viewer to embed online documents into your web page. Step 1: use the iframe tag to create an “internal” frame within your document So the essential part of embedding online documents into your we page is to use the iframe tag in your html. The iframe tag accepts 3 x attributes: “src”, “width” and “height” If you are interested in removing the border from the iframe tag simply do this by the frameBorder attribute (remember - frameBorder is case-sensitive): frameBorder=”0” Step 2: Use the Google Docs link within your src attribute This is the Google docs link that you will append to the 'src' section of the iframe tag: https://docs.google.com/gview? So that all you need to do is supply the URL that Google docs view needs to point to. In this example, we are interested in embedding the following document, which is a Microsoft PowerPoint presentation that you can download from the Adobe site: https://www.adobe.com/suppor...

Using SQLite in C# / .NET Environments

Image
A quick how-to guide on setting up and using SQLite in C# / .NET environments. Nothing complicated just instructions on installing SQLite and using its APIs for database creation and querying. Much credit must go to this splendid link on helping me understand SQLite's usage: http://blog.tigrangasparian.com/2012/02/09/getting-started-with-sqlite-in-c-part-one/ Step 1. Create a Visual Studio console application Step 2: Download the SQLite code For programs running in .NET in Visual Studio install as a NuGet package: Select Tools > NuGet Package Manager > Package Manager Console: Enter install-package System.Data.SQLite at the PM prompt: Otherwise all the other SQLite downloads (Windows, Linux etc) are available here: https://www.sqlite.org/download.html Step 3: Implement the SQL APIs for database creation, querying etc Some common SQLite C# commands Create a database file: [code language="csharp"] SQLiteConnection.CreateFile(DatabaseFile...

How to use delegates in C#

Image
A simple guide to using delegates in C#. It should be suitable for anybody but is particularly aimed at C/C++ programmers who are more used to using function pointers and function objects as a means of passing function parameters. For the C/C++ equivalent of using callbacks see this link: https://www.technical-recipes.com/2016/how-to-write-callbacks-in-c/ Step 1: declare the delegate [code language="csharp"] public delegate int Transform(int value); [/code] Step 2: create methods for the delegate [code language="csharp"] public int DoubleValue(int value) { return value * 2; } public int SquareValue(int value) { return value * value; } [/code] Step 3: Instantiate the delegate and use it [code language="csharp"] TransformValue doubleValue = DoubleValue; [/code] Full Code listing showing example usage [code language="csharp"] using System; namespace Delegates { internal class Program { #region Delegates...

Using Timers in C#

Image
Example 1: In Windows Forms applications Create a new Windows Forms Application: For WinForms applications we make use of the System.Windows.Forms.Timer [code language="csharp"] private static readonly Timer Timer = new Timer(); [/code] Some common Timer tasks are to: 1. Subscribe to the event that is called when the interval has elapsed: [code language="csharp"] Timer.Tick += TimerEventProcessor; [/code] 2. Set the Timer interval: [code language="csharp"] Timer.Interval = 5000; [/code] 3. Start the timer (these are equivalent): [code language="csharp"] Timer.Start(); Timer.Enabled = true; [/code] 4. Stop the timer (these are equivalent): [code language="csharp"] Timer.Stop(); Timer.Enabled = false; [/code] Example Usage: [code language="csharp"] using System; using System.Windows.Forms; namespace TimerAppWinForms { public partial class Form1 : Form { private const string Capti...

How to make an HTTP request to an HTTP-based service in C#

Image
A minimalist explanation as follows. 1. Create the HTTP Web Request Create the WebRequest object from the from the Universal Resource Identifier (URI) you supply eg [code language="csharp"] const string Url = "http://www.microsoft.com"; var webRequest = WebRequest.Create(Url); [/code] 2. Send the HTTP request and wait for the response [code language="csharp"] var responseStream = webRequest.GetResponse().GetResponseStream(); [/code] 3. Create a StreamReader object The StreamReader object is used to read back the text characters from the HTTP response: I use the ‘using’ statement as a convenient means of ensure the object is subsequently disposed. [code language="csharp"] using(var streamReader = new StreamReader(responseStream)) { // Return next available character or -1 if there are no characters to be read while (streamReader.Peek() > -1) { Console.WriteLine(streamReader.ReadLine()); } ...

How to write callbacks in C++

Image
A simple guide to writing function callbacks in C++. For the C# equivalent of using delegate see this link: https://www.technical-recipes.com/2016/how-to-use-delegates-in-c The steps you need to take are: 1. Declare the function pointer that will be used to point to functions of given return types/argument(s) In this example a pointer to function that takes and int and returns an int: [code language="cpp"] typedef int(*fptr)(int); [/code] 2. Create the example functions that will be pointed to by the function pointer In these examples they functions that will be used to transform your number: [code language="cpp"] int DoubleValue(int value) { return value * 2; } int SquareValue(int value) { return value * Value; } [/code] 3. Apply the callback usage In this case write a function that accepts that the function pointer as well as the argument(s) used by that function pointer: [code language="cpp"] int TransformValue(int value...

Serializing data in C# using Migrant

Image
Antmicro Migrant is available from here: https://github.com/antmicro/Migrant To get started do the following steps: 1. Create a new console application in Visual Studio: 2. Install the ‘Migrant’ NuGet package Select Tools > NuGet Package Manager > Package Manager Console: Install Migrant by typing ‘install-package migrant’ 3. A simple serialization example. Modify your Program.cs to perform a basic serialize / deserialize example as shown: [code language="csharp"] using System; using System.IO; using Antmicro.Migrant; namespace MigrantSerialize { internal class Program { private static void Main(string[] args) { var serializer = new Serializer(); var stream = new MemoryStream(); var obj = new MyClass { Name = "XYZ", Value = 111 }; serializer.Serialize(obj, stream); stream.Seek(0, SeekOrigin.Begin); var deserializedObj = serial...

Using the .NET WebBrowser Control in a WinForms project

Image
Very easy to do. Just execute the following steps: 1. Create a new WinForms Project 2. Add the WebBrowser control from the ToolBox. Select View > ToolBox. Select WebBrowser in the Common Controls: 3. Drag the WebBroswer control on to your form Like so: 4. Add Event handling methods. Add an event for the form loaded: 5. Update the event handling code In Form.cs updates as follows. I have also included the option to apply zooming to the selected web page: [code language="csharp"] using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WebBrowser { public partial class Form1 : Form { public Form1() { InitializeComponent(); webBrowser1.Navigate("www.google.co.uk"); } private void Form1_Load(object sender, EventA...