Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Monday, November 12, 2012

Browsing the GAC

GAC in fact is here: C:\Windows\Microsoft.NET\assembly\GAC_MSIL 

Obsolete but possibly still useful old trick from http://msmvps.com/blogs/bill/archive/2005/09/20/66921.aspx

You can make it possible to browse the GAC like a set of folders and files and hence access assemblies that are only in the GAC.  Typically I just use a command prompt to do this, but there's a registry hack which lets you do it from explorer:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Fusion]
"DisableCacheViewer"=hex:10
Toogle the value to 0 to go back to the usual view, non zero for folder and file view.  Now when you do that you might find some interesting assemblies.  My favourite at the moment that comes with VS 2005, is:
C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.Zip
Oh, and an interesting side effect of this is you can now browse to a file in the GAC and add it as a reference to your Visual Studio project.

Monday, May 2, 2011

MVC3, Razor and other cool stuff

I made presentation on Sarasota Barcamp 2011.
For the presentation I created temporary web site greatsarasota.com.
Most resources can be found on http://greatsarasota.com/Home/About

Wednesday, April 6, 2011

Delete directory in IsolatedStorage

IsolatedStorageFile.DeleteDirectory works with empty directories only.
I wrote the method for recursive deleting content of the directory:

using System.IO.IsolatedStorage;
. . .
        public void DeleteDirectory(IsolatedStorageFile fs, String dirName)
        {
            String pattern = dirName + @"\*";
            String[] files = fs.GetFileNames(pattern);
            foreach (String fName in files)
            {
                fs.DeleteFile(Path.Combine(dirName, fName));
            }
            String[] dirs = fs.GetDirectoryNames(pattern);
            foreach (String dName in dirs)
            {
                DeleteDirectory(fs, Path.Combine(dirName, dName));
            }
            fs.DeleteDirectory(dirName);
        }

Usage:
            using (IsolatedStorageFile fs = IsolatedStorageFile.GetUserStoreForApplication())
            {
                DeleteDirectory(fs, @"\Projects"); // directory "Projects" in the root will be deleted
            }


Wednesday, February 16, 2011

Pluralize English Nouns

Pluralize English Nouns

Pluralize English Nouns

By KodefuGuru9. December 2010 18:56
It’s a common problem: you have a noun that needs to be pluralized (or singularized!), so you end up rolling a custom solution or go with the dreaded (s) moniker. I recently discovered that it is in fact easy: the .NET Framework will pluralize or singularize nouns for you.

Thursday, January 14, 2010

LINQ to XML sample: get value with default (if not exists)

XElement dummy = new XElement("dummy");
if (xe.Descendants("Response").Descendants("Status").DefaultIfEmpty(dummy).First().Value == "ok"){}

Tuesday, January 12, 2010

LINQ to XML tiny sample


// load from file
XElement resp = XElement.Load("RCExtResponse.xml");
var all = from r in resp.Descendants()
          where r.Attributes().Count() == 0
          select r;

// or load from string
XElement xe = XElement.Parse(xmlString);

// use default

var caller =
    (from e in xe.Descendants("PracticeName")
     select (String)e).DefaultIfEmpty("");


Monday, November 30, 2009

Nullables


String filter = param as String ?? String.Empty;

Tuesday, October 13, 2009

Colors. Part 2.

Enumerate all Known Colors and put them in the dictionary
Dictionary<KnownColor, int> colorsRGB;
can be done this way:
int
    numAliceBlue = (int)KnownColor.AliceBlue,
    numYellowGreen = (int)KnownColor.YellowGreen,
    numColors = numYellowGreen - numAliceBlue + 1;
colorsRGB = new Dictionary<KnownColor, int>(numColors);
for (int i = numAliceBlue; i <= numYellowGreen; i++)
{
    KnownColor kc = (KnownColor)i;
    colorsRGB.Add(kc, Color.FromKnownColor(kc).ToArgb());
}

Then I can sort the collection using LINQ:
Sort by name:
var sortedColors = (from clr in colorsRGB
                    orderby clr.Key
                    select clr).ToArray();
by RGB value:
var sortedColors = (from clr in colorsRGB
                   orderby clr.Value descending
                   select clr).ToArray();

I created a function ColorWeight to “weight” a color (just sum of R, G, and B components),
And even can sort by the “weight”:
var sortedColors = (from clr in colorsRGB
                    orderby ColorWeight(clr.Key) descending
                    select clr).ToArray();

I do like LINQ!

For the test I draw buttons with KnownColor as background. I wanted to have foreground color to be recognizable on the background.
This turn out to be not easy task. I did not want to mess with non-RGB presentations (like HSV or HSL) which would simplify the task.
At first I tried “inverted” color:
return Color.FromArgb((int)backColor.ToArgb() ^ 0xF0F0F0);
It did not work well.
So I created “color weight”
private int ColorWeight(Color color)
{
    int bColor = (int)color.ToArgb(),
        r = (bColor & 0x00FF0000) >> 16,
        g = (bColor & 0x0000FF00) >> 8,
        b = bColor & 0x000000FF;
    return r + g + b;
}

It is not perfect, but kind of fit the purpose: after all I put Black text if “weight” > 0x60+0x60+0x60 and White text otherwise.
This is the result:



Monday, October 12, 2009

Colors. Part 1.

First 27 and last 7 of System.Drawing.KnownColor are “System” colors:

for (int i = 1; i <= Enum.GetValues(typeof(KnownColor)).Length; i++){
    if (i <= 27 || i >= 168){
        String str = String.Format("{0}.{1}", i, (KnownColor)i);
        System.Diagnostics.Trace.WriteLine(str);
    }
}

1.ActiveBorder
2.ActiveCaption
3.ActiveCaptionText
4.AppWorkspace
5.Control
6.ControlDark
7.ControlDarkDark
8.ControlLight
9.ControlLightLight
10.ControlText
11.Desktop
12.GrayText
13.Highlight
14.HighlightText
15.HotTrack
16.InactiveBorder
17.InactiveCaption
18.InactiveCaptionText
19.Info
20.InfoText
21.Menu
22.MenuText
23.ScrollBar
24.Window
25.WindowFrame
26.WindowText
27.Transparent
168.ButtonFace
169.ButtonHighlight
170.ButtonShadow
171.GradientActiveCaption
172.GradientInactiveCaption
173.MenuBar
174.MenuHighlight

ENUMs

Using bitwise enums:

[Flags]
enum Color : long {
    Black=0,
    Red=1,
    Green=2,
    Blue=4,
    Orange = Red | Green,
    White = Red |Green | Blue
}

Wednesday, October 7, 2009

Variable number of parameters in method (function)

Variable number of parameters in method:
Use params in declaration, use comma-separated list or array when calling.

Friday, October 2, 2009

Extension methods

It was not that difficult to create extension after all.

At one moment I found out that String type do not allow me to surround a string with double quotas.
So I wrote the following extension to String type:
public static class Extensions
{
    public static String SurroundWith(this String str, Char chr)


    {
        return chr + str + chr;
    }
}

This extension I can then use in my program:
String fileName =
@"C:\Program Files (x86)\Microsoft Office\OFFICE11\Excel.exe";
fileName = fileName.SurroundWith('"');

fileName = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.csv");
Process.Start(new ProcessStartInfo("Excel.exe", fileName.SurroundWith('"')));