Grab the Facebook C# SDK at http://csharpsdk.org/ !
Tag Archives: C#
Get information from Team Foundation Server (TFS) via C# code
This code shows you how you could interact with your team foundation server. In my case a TFS 2010.
Open a new console application in Visual Studio and add these references:
- Microsoft.TeamFoundation.Client
- Microsoft.TeamFoundation.VersionControl.Client
TeamFoundationServer tfs = new TeamFoundationServer("http://yourtfsserver:port/something");
List<string> changedFiles = new List<string>();
VersionControlServer VCServer = (VersionControlServer)tfs.GetService<VersionControlServer>();
try
{
string path = @"C:\Users\<username>\Documents\Visual Studio 2010\Projects\just the path to your solution";
VersionSpec version = VersionSpec.Latest;
int deletionId = 0;
RecursionType recursion = RecursionType.Full;
string user = @"domain\yourusername";
foreach (Changeset item in VCServer.QueryHistory(path, version, deletionId, recursion, user, null, null, Int32.MaxValue, true, false, true))
{
foreach (Change c in item.Changes)
{
// c.Item.ServerItem;
}
}
}
catch { }
Happy coding ;)
C# Linq: Zip method
string[] brands = new string[] { "BMW", "Mercedes", "Audi" };
string[] websites = new string[] { "www.bmw.com", "www.mercedes.com", "www.audi.com" };
var output = brands.Zip(websites, (b, w) => string.Format("{0} can be found at {1}", b, w));
foreach (var line in output)
Console.WriteLine(line);
This gives you:
BMW can be found at www.bmw.com Mercedes can be found at www.mercedes.com Audi can be found at www.audi.com
More information at: http://msdn.microsoft.com/en-us/library/dd267698.aspx
Rx (Reactive Extensions)
Reactive Extensions (Rx) was added to Codeplex.com:
The project is actively developed by the Microsoft Open Technologies, inc., in collaboration with a community of open source developers.
The Reactive Extensions (Rx) is a library for composing asynchronous and event-based programs using observable sequences and LINQ-style query operators. Using Rx, developersrepresent asynchronous data streams with Observables , query asynchronous data streams usingLINQ operators, and parameterize the concurrency in the asynchronous data streams usingSchedulers. Simply put, Rx = Observables + LINQ + Schedulers.
Find out more on: https://rx.codeplex.com/
Making Instant C# Viable – Visualization
A project from Ermau: Research into instant feedback in software engineering.
Check out the cool video at http://ermau.com/making-instant-csharp-viable-visualization/.
Find out more at:
C#: Save a canvas as an image
This C# code saves a Canvas as a *.PNG image:
private void CreateSaveBitmap(Canvas canvas, string filename)
{
RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
(int)canvas.Width, (int)canvas.Height,
96d, 96d, PixelFormats.Pbgra32);
// needed otherwise the image output is black
canvas.Measure(new Size((int)canvas.Width, (int)canvas.Height));
canvas.Arrange(new Rect(new Size((int)canvas.Width, (int)canvas.Height)));
renderBitmap.Render(canvas);
//JpegBitmapEncoder encoder = new JpegBitmapEncoder();
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
using (FileStream file = File.Create(filename))
{
encoder.Save(file);
}
}
Use it like this:
CreateSaveBitmap(myCanvas, @"C:\temp\out.png");
ASP.NET Web API
ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework.
Can’t wait for the full version of MVC 4! :-)
[ link ]
ActiveMesa MathSharp: convert your math to code
Have you ever had to convert a formula in a Word document to code? Ever wished that it could somehow be automated? With MathSharp, this is possible!
MathSharp is a program for automatic conversion of math equations from MathML (produced by programs such as Word or MathType) into code in languages such as C# or F#.
[ link ]
ASP.NET MVC: place JavaScript from a View inside the head-section by using sections
When working with ASP.NET MVC, you often need some JavaScript in your views.
The most obvious way of doing this is just adding the JavaScript inside the view:
@{
ViewBag.Title = "About Us";
}
<script type="text/javascript">
// some code
</script>
<h2>About</h2>
<p>
Put content here.
</p>
Your html source code will end up with a mess:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>About Us</title>
<link href="/Content/Site.css" rel="stylesheet" type="text/css" />
<script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
<script src="/Scripts/modernizr-1.7.min.js" type="text/javascript"></script>
</head>
<body>
<div>
<header>
<div id="title">
<h1>Title</h1>
</div>
<div id="logindisplay">
[ <a href="/Account/LogOn">Log On</a> ]
</div>
<nav>
<ul id="menu">
<li><a href="/">Home</a></li>
<li><a href="/Product">Products</a></li>
<li><a href="/Home/About">About</a></li>
</ul>
</nav>
</header>
<section id="main">
<script type="text/javascript">
// some code
</script>
<h2>About</h2>
<p> Put content here. </p>
</section>
<footer>
</footer>
</div>
</body>
</html>
As you can see in the code above, the JavaScript was added where the your view was rendered.
To keep your code clean, you can use and render sections!
Open your master View _Layout.cshtml and add a RenderSection:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
@if (IsSectionDefined("MyOtherJavascript"))
{
@RenderSection("MyOtherJavascript");
}
</head>
<body>
Now in your View (in my case /Home/About.cshtml) add the section:
@{
ViewBag.Title = "About Us";
}
@section MyOtherJavascript {
<script type="text/javascript">
// some code
</script>
}
<h2>About</h2>
<p>
Put content here.
</p>
Now your JavaScript will be injected inside the header:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>About Us</title> <link href="/Content/Site.css" rel="stylesheet" type="text/css" /> <script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script> <script src="/Scripts/modernizr-1.7.min.js" type="text/javascript"></script> <script type="text/javascript"> // some code </script> </head> <body> <!-- ... -->
That’s it! ![]()
You can add more logic to it, there is also an overload of the RenderSection in case you just want to say it is optional or not.
I demonstrated the RenderSection by injecting JavaScript into the header, but this can be used in other places (and you can also inject and render normal html code).
FYI: I’m using ASP.NET MVC 3 with Razor engine.
[ msdn ]
C# Read and filter lines from file
Something useful in C# from .NET 4.0:
File.ReadLines("c:\test.txt").Where(x => !x.StartsWith("xYz--"));
File.ReadLines("c:\test.txt").AsParallel().Where(x => !x.StartsWith("xYz--"));
[ msdn ]
