February 23rd, 2011
It has been some weeks ago when I needed to find a way to make to be executed with full rights. There are several reason why you got to do so e.g. get write-access to C:
It was my aim to show that nice UAC Dialog from Windows itself when the rights are needed.
And this is how it works:
Create a manifest for you executable which is simple xml:
<?xml version="1.0" encoding="utf-8" ?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0"
processorArchitecture="X86"
name="yourApp"
type="win32" />
<description>Your App Description</description>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3"><security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator" />
</requestedPrivileges>
</security>
</trustInfo></assembly>
this is the important line:
<requestedExecutionLevel level="requireAdministrator" />
Now you can rename it to (YourApp).manifest. The .NET Framework when executing the file will see the Manifest and handle its contents.
Or you can, which I regard as better, embed the .manifest file into you executable.
mt -manifest YourApp.exe.manifest -outputresource:YourApp.exe
The mt.exe come with Visual Studio SDK which can be downloaded at Microsoft’s.
To simplify this I created a Post-Build event which looks like this:
“C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\mt.exe” -manifest “$(ProjectDir)$(TargetName).exe.manifest” –outputresource:”$(TargetDir)$(TargetFileName)”;#1
An important note:
If your assembly is strong named, you will be unable to embed the manifest into it as it would invalidate the strong naming.
Some source:
Every assembly, whether static or dynamic, contains a collection of data that describes how the elements in the assembly relate to each other. The assembly manifest contains this assembly metadata. An assembly manifest contains all the metadata needed to specify the assembly’s version requirements and security identity, and all metadata needed to define the scope of the assembly and resolve references to resources and classes. The assembly manifest can be stored in either a PE file (an .exe or .dll) with Microsoft intermediate language (MSIL) code or in a standalone PE file that contains only assembly manifest information. (http://msdn.microsoft.com/en-us/library/bb756929.aspx)
February 23rd, 2011
I today discovered something I wanted to share with you:
When normalizing strings, it is highly recommended that you use ToUpperInvariant instead of ToLowerInvariant because Microsoft has optimized the code for performing uppercase comparisons.
ToUpperInvariant is preferred because it makes all characters round-trip. See msdn.microsoft.com/en-us/library/bb386042.aspx. For comparisons, write
"a".Equals("A",StringComparison.OrdinalIgnoreCase)
In 99,9 percent of all cases it doesn’t matter that much.
I tried benchmarking ToUpperInvariant vs ToLowerInvariant. I cannot find any difference in their performance under .NET 2.0 or 3.5. Certainly not anything that warrant “highly recommending” using one over the other.
May 19th, 2010
I was fed up with declaring a “helper” variable for every foreach in which I needed a counter.
The solution is very cool I think:
My list of strings I’d like to iterate:
List<string> myStrings = new List<string>()
{
"abc",
"def",
"xyz"
};
Old style:
int index = 0;
foreach (string s in myStrings)
{
Console.WriteLine("{0}: {1}", index++, s);
}
cool way using .net 3.5’s lambda syntax:
foreach (var o in myStrings.OfType<object>().Select((x, i) => new { x, i }))
{
Console.WriteLine("{0}: {1}", o.i, o.x);
}
August 12th, 2009
I was recently talking about linq features with a friend. Now I saw somebody wanted to break a loop after 50 iterations. Trivial but also for that there’s a precious solution using linq.
int processed = 0;
foreach(ListViewItem lvi in listView.Items)
{
//do stuff
++processed;
if (processed == 50) break;
}
use linq
foreach(ListViewItem lvi in listView.Items.Take(50))
{
//do stuff
}
or, you’re right “old” style would be
for(int i=0; i < listView.Items.Count && i <= 50; i++)
{
ListViewItem lvi = listView.Items[i];
//do stuff
}
July 1st, 2009
As you know you can query with LINQ lots of different sourcse. It’s possible to query, project and filter data in arrays, enumerable classes, XML (XLINQ), relational database, and third party data sources. Last year I among others wrote something about LINQ & XML.
After doing some query we get results of a as a collection of in-memory objects that can be enumerated using a standard iterator function such as C#’s foreach.
I found a funny Provider called Linq to Amazon which allows querying Amazon for books using Linq! It uses Linq’s extensibility to allow for language-integrated queries against a book catalog. The Linq query gets converted to REST URLs supported by Amazon’s web services. These services return XML. The results are converted from XML to .NET objects using Linq to XML.
For the moment, let’s look at the client code:
var query =
from book in new Amazon.BookSearch()
where
book.Title.Contains("darkness and light") &&
(book.Publisher == "Hitchcock") &&
(book.Price <= 25) &&
(book.Condition == BookCondition.New)
select book;
I think this code speaks for itself! This is Linq to Amazon code. It expresses a query against Amazon, but does not execute it… The query will be executed when we start enumerating the results.
The following piece of code converts from Linq to Amazon to Linq to Objects:
var sequence = query.ToSequence();
The var might remind you to the times of Flash, VB or JavaScript. But those times were baaad, very bad. Nowadays we’re strongly typed or even better generic:
A good alternative is:
IEnumerable<SomeOtherClass> results = ...
The returned catalogue can now be grouped, filtered more detailed or just thrown away. Do whatever you like todo
—
There are many more, more or less usefull ones:
- LINQ to SharePoint
- LINQ to Amazon
- LINQ to Active Directory (LDAP)
- LINQ to NHibernate
- LINQ to MySQL / Oracle / SQLite
- LINQ to Flickr
Have fun
July 14th, 2008
In C#, there is no String.Left() function. Fair enough; we can roll up our sleeves and write our own function lickety-split:
public static string Left(string s, int len)
{
if (len == 0 || s.Length == 0)
return "";
else if (s.Length <= len)
return s;
else
return s.Substring(0, len);
}
And call it like so:
var s = "superlongstring";
s = Left(s, 5);
But with the advent of C# 3.0, there’s an even better way — extension methods. With an extension method, we “extend” the String to add the missing function. The code is fairly similar:
public static string Left(this string s, int len)
{
if (len == 0 || s.Length == 0)
return "";
else if (s.Length <= len)
return s;
else
return s.Substring(0, len);
}
And now we can call it as if this very method existed on the String class as shipped:
var s = "superlongstring";
s = s.Left(5);
Pretty slick. It’s difficult not to fall in love with extension methods, as they allow you to mold classes into exactly what you think they should be. This is fairly innocuous in C#, as extension methods only allow you to add new functionality to classes, not override, remove, or replace anything.