How to allow only one application instance
The following code will ensure that your application can only have one instance active. If the user tries to open the application again while the application is already running then the application will simply quit (in this case after showing a message).
For this to work we will need to modify the Program.cs to check with the use of Mutex if the application is already running or not before we open the main form. Our check will take place in the Main method.
Your code in Program.cs should look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | using System; using System.Reflection; using System.Runtime.InteropServices; using System.Threading; using System.Windows.Forms; namespace WindowsFormsApplication1 { static class Program { [STAThread] static void Main() { /* Retrieve the application Guid attribute. Alternative you can simply use the application name to initialize the Mutex * but it might be risky as other programs might have similar name and make use of the Mutex class as well. */ GuidAttribute appGuid = (GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute), true)[0]; // The boolean that will store the value if Mutex was successfully created which will mean that our application is not already running. bool createdNew = true; // Initialize the Mutex object. using (Mutex mutex = new Mutex(true, appGuid.Value, out createdNew)) { /* If Mutex was created successfully which means our application is not running run the application as usual * or else display a message and close the application.*/ if (createdNew) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } else { MessageBox.Show("Application is already running"); } } } } } |
Feel free to modify the example to suit your needs.
Get value between two strings
You might find this snippet particular useful in cases where you want to get a value between two other values.
This takes advantage of the string’s Split
overload to pass an array of two values. This will result in the string being split twice, once for the first value in the array and once more for the second value. The result will be that the second value in the array that Split
returns is the actual value between the first and the second value we passed as argument.
Code:
1 2 3 4 5 6 7 8 9 10 11 | private string GetBetween(string strSource, string strStart, string strEnd) { string result = string.Empty; if (strSource.Contains(strStart) && strSource.Contains(strEnd)) { result = strSource.Split(new string[] { strStart, strEnd }, StringSplitOptions.None)[1]; } return result; } |
Example:
1 2 3 4 | string source = "this is (my value) a test"; // Returns "(my value)" GetBetween(source, "this is ", " a test"); |
How to edit a file in Program Files
Windows User Account Control (UAC) is a security infrastructure introduced with Windows Vista and has been included in all Windows versions after that so far. UAC limits the access to application software to ensure that they cannot perform actions that might harm the system in any way.
Because of how UAC works you might have noticed a few times that you are unable to edit specific files, getting an access denied error. The reason for that, as stated above, is because UAC is limiting the access you have to particular folders and files, allowing you in most cases just to view them.
In order to be able to edit a file that is protected by UAC you will need to access that file while having administrator rights. Luckily there is an easy functionality provided by Microsoft for that. Right click the application, file or folder you want to run as administrator and select the Run as administrator option.
That will give administration rights to the program/file you want to run, allowing you to modify the contents where in other cases would be impossible because of UAC.
Posted in Software Tips.
Tagged UAC, User Account Control, Windows UAC
How to disable FlashPlayerPlugin process
With the release of Flash player 11.3 Adobe has changed their player so it uses a different process when playing flash videos in the browser. The main reason for that was to ensure that if for whatever reason the flash player encounters a problem the browser will be unaffected. In some cases users expressed that the new process was causing issues for them which means that the ideal thing to do in that case is to disable flash’s protected mode.
In order to disable flash’s protected mode follow the next steps:
- Navigate to C:\Windows\SysWOW64\Macromed\Flash (Windows 64-bit systems) or C:\Windows\System32\Macromed (Windows 32-bit systems) depending on what Windows bit version you are running
- Locate and open the file with the name mms.cfg
- Then simply change the line
ProtectedMode
line from ProtectedMode=1 to ProtectedMode=0 - Restart your browser
Now flash player should no longer start a separate process everytime a flash video is being played in your browser.
Posted in Software Tips.
Tagged Adobe, flash player, FlashPlayerPlugin, ProtectedMode
How to get the short path of a directory
In some occasions it might be necessary to use a directory’s short path. This is possible with the use of the Windows GetShortPathName
function.
Implementation:
1 2 3 4 5 6 7 8 9 | [DllImport("kernel32.dll", CharSet = CharSet.Auto)] public static extern uint GetShortPathName(string lpszLongPath, StringBuilder lpszShortPath, uint cchBuffer); private static string GetShortPath(string longPath) { StringBuilder shortPath = new StringBuilder(255); GetShortPathName(longPath, shortPath, 255); return shortPath.ToString(); } |
Simply call the GetShortPath
with the directory you want to get the short path as an argument.
Example:
1 | string applicationShortPath = GetShortPath(Application.ExecutablePath); |
Posted in C#.
Tagged C#, csharp, GetShortPathName, short path, snippet, winforms