Localstorage object storage
Don’t remember where I got this snippet of code but it’s pretty useful so thanks to however wrote it
.
This script let’s you persist any javascript object in the localstorage with a key and a json string.
if window.Storage and window.JSON window.$storage = (key) -> set: (value) -> localStorage.setItem(key, JSON.stringify(value)) get: -> item = localStorage.getItem(key) JSON.parse(item) if item
And it’s used as follows:
$storage('test').set({"hello": "world"}) alert $storage('test').get().hello
Visual Studio 11 CSS3 Parser
Was spelunking in the folders of my Visual Studio 11 installation a couple of weeks ago and found Microsoft.CSS.Core.dll (located in C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\CommonExtensions\Microsoft\Web\CSS3).
I was intrigued and needed to look deeper and what I found was a CSS parser that you can use from your application e.g. I needed a CSS unpacker (that turns a minimized CSS file into a readable format). With the built in formatter I was able to do this:
static void Main(string[] args) { if (args.Length < 2) { Console.WriteLine("cssunpack [input] [output]"); Environment.Exit(-1); } var parser = new CssParser(); var css = parser.Parse(File.ReadAllText(args[0]), insertComments: false); var formatter = new CssFormatter(); File.WriteAllText(args[1], formatter.Format(css)); }
So if we do some small tweaks to the CssFormatterOptions we can build a minimizer as well:
static void Main(string[] args) { if (args.Length < 2) { Console.WriteLine("csspack [input] [output]"); Environment.Exit(-1); } var parser = new CssParser(); var css = parser.Parse(File.ReadAllText(args[0]), insertComments: false); var formatter = new CssFormatter { Options = new CssFormattingOptions { IndentType = IndentType.Spaces, IndentSize = 0, SpacesPerTab = 0, InitialIndentString = string.Empty, QuoteType = QuoteType.Double, BlockBracePosition = BracePosition.Compact, SortProperties = false, CompactBlocks = false, CompactBlockThreshold = 0, RemoveEscapes = true, ConvertColorsToHex = true, CompressColors = true, ElementSelectorCasing = Casing.Lowercase, PropertyNameCasing = Casing.Lowercase, RemoveLastSemicolon = true, IndentRuleHierarchy = false, ForStyleBlock = true } }; File.WriteAllText(args[1], formatter.Format(css).Replace(Environment.NewLine, "")); }
There is a lot of more features so take a look for yourself.
I don’t know about the license but it’s probably ok to use on your local dev machine if you have VS11 installed.
Windows 8 Start Menu
At //BUILD/ some people are asking if it is possible to get the normal start menu back, and yes it is follow these instruction:
NOTE: This is a hack and will break stuff.
- Launch Regedit
- Navigate to \HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer
- Change RPEnabled from 1 to 0
- Log out
//BUILD/ day -1
So it’s Monday the conference starts tomorrow and I’m super stoked, to burn some time we went to the Citadel outlet doing some shopping and Starbucks coffee drinking.
In the evening we where invited to the Microsoft Sweden meet up. It was a fun event, and everyone got matching jackets so all swedes can be singled out
//BUILD/ day -2
Todays prebuild theme is jetlag, we have a lovely –9 hours time different from Sweden
So we are spending the day at a shopping mall (a.ka Starbucks checking emails and twitter)
Catching the Close button event
Got a question yesterday how to get an event when a user clicks the close button in a console app. This code snippet is what I have used.
class Application { static Application() { SetConsoleCtrlHandler(Handler, true); } public static event Action Close; public static event Action Break; public static event Action CtrlC; public static event Action LogOff; public static event Action Shutdown; [DllImport("Kernel32")] private static extern bool SetConsoleCtrlHandler(HandlerRoutine handler, bool add); private delegate bool HandlerRoutine(CtrlTypes CtrlType); private enum CtrlTypes { CTRL_C_EVENT = 0, CTRL_BREAK_EVENT, CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT = 5, CTRL_SHUTDOWN_EVENT } private static bool Handler(CtrlTypes ctrlType) { switch (ctrlType) { case CtrlTypes.CTRL_C_EVENT: if (CtrlC != null) CtrlC(); break; case CtrlTypes.CTRL_BREAK_EVENT: if (Break != null) Break(); break; case CtrlTypes.CTRL_CLOSE_EVENT: if (Close != null) Close(); break; case CtrlTypes.CTRL_LOGOFF_EVENT: if (LogOff != null) LogOff(); break; case CtrlTypes.CTRL_SHUTDOWN_EVENT: if (Shutdown != null) Shutdown(); break; default: throw new ArgumentOutOfRangeException("ctrlType"); } return true; } }
And it’s used as follows.
static void Main() { Application.Close += () => { Debugger.Break(); }; Console.ReadKey(); }
Close database connections from TSQL
I always forget how to do this so I thought someone else probably have the same issue ![]()
alter database DatabaseName set single_user with rollback immediate go
GodModes in Windows 7
GodMode is a undocumented feature in Windows 7 that gives you direct access to all kind of different settings in windows. The most popular is the folder view of all available commands in the Control Panel. To get access to this you need to do the following.
- Create a new folder
- Name the folder: GodMode.{ED7BA470-8E54-465E-825C-99712043E01C}
Now when you open the folder you will get the following folder view:
Bellow is a list of other direct access folders that you can create as the one above.
- ControlPanel.{ED7BA470-8E54-465E-825C-99712043E01C}
- LocationSensor.{00C6D95F-329C-409a-81D7-C46C66EA7F33}
- BiometricDevice.{0142e4d0-fb7a-11dc-ba4a-000ffe7ab428}
- PowerOptions.{025A5937-A6BE-4686-A844-36FE4BEC8B6D}
- TaskbarIcons.{05d7b0f4-2121-4eff-bf6b-ed3f69b894d9}
- Credentials.{1206F5F1-0569-412C-8FEC-3204630DFB70}
- InstallFromNetwork.{15eae92e-f17a-4431-9f28-805e482dafd4}
- DefaultPrograms.{17cd9488-1228-4b2f-88ce-4298e93e0966}
- PublicKeys.{1D2680C9-0E2A-469d-B787-065558BC7D43}
- WifiNetworks.{1FA9085F-25A2-489B-85D4-86326EEDCD87}
- Network.{208D2C60-3AEA-1069-A2D7-08002B30309D}
- Computer.{20D04FE0-3AEA-1069-A2D8-08002B30309D}
- Printers.{2227A280-3AEA-1069-A2DE-08002B30309D}
- WorkplaceConnetions.{241D7C96-F8BF-4F85-B01F-E2B043341A4B}
- Firewall.{4026492F-2F69-46B8-B9BF-5654FC07E423}
- PerformanceRatings.{78F3955E-3B90-4184-BD14-5397C15F1EFC}
Frame rate counters in WP7
1. Render Thread FPS![]()
The number of frames per second that the independent simple animations and rendering thread is using. Keeping around 60 will provide a great experience, while a number of 30 fps will begin to show a poor experience to the end user.
2. User Interface Thread FPS
The number of fps that the primary user interface thread is experiencing. Property change notifications, data binding, primary managed code execution, and animations not handled on the render thread use this threads resources.
3. Textual Memory Usage
A specialized memory counter indicating the video memory used for storing application textures.
4. Surface Counter
A counter of the number of surfaces that are passed to the graphics chip
5. Intermediate Texture Count
The number of intermediate textures created for compositing.
6.Screen Fill Rate
A metric representing the number of complete phone screens being painted each and every frame.
| Counter | Ideal Minimum | Best Experience | Theoretical Max |
| Render Thread | 30 fps | 60 fps | 120 fps |
| UI Thread | 15 fps | > 15 fps | 120 fps |
| Screen Fill Rate | <= 2.0 | N/A |
Relative paths and Windows Services
So you have converted your console app to a Windows Service but now all your relative paths is relative to C:\Windows\system32 (or c:\Windows\SysWOW64)
.
This is because your service is executed by svchost.exe that is located in C:\Windows\system32 (or svchost.exe located in c:\Windows\SysWOW64 if you run a 32bit service on a 64bit Windows) and therefore has it as executing directory.
To resolve this I usually use the ResolvePath method bellow:
public static string ResolvePath(string path) { if (!Path.IsPathRooted(path)) { var executingAsmPath = new UriBuilder(Assembly.GetExecutingAssembly().CodeBase).Path; var directoryName = Path.GetDirectoryName(Uri.UnescapeDataString(executingAsmPath)); path = Path.Combine(directoryName, path); } return path; }
either resolving the path directly:
string logPath = ResolvePath(@".\log.txt");
or changing the current directory:
Directory.SetCurrentDirectory(ResolvePath(""));









