How to extract decimal positions of a number

by Naveen 14. July 2010 06:24

Today a Silverlight programmer posted a question on forums about extracting tens, thousands etc. values from a given number. I will post part of the questions here as well.


I'm working on a Silverlight project in Blend 4, and I'd like to take an arbitrary
int variable and extract the value of each decimal position - ones, tens, hundreds, thousands etc. 

Example:
So, basically I need a method that take a variable:
i = 948563
and returns the variables:
j = 3
k = 60
l = 500
m = 8000
n = 40000
o = 900000
All natural numbers, no fractions.

Since this was about natural numbers, a simple and crude solution without getting into math of finding remainders etc. is to simple convert the input number to string and then walk that string in reverse direction to extract each character and then multiply it with exponent of 10 based on its position. Here is code for a simple console application that demonstrates this.


class Program
{
 static void Main(string[] args)
 {
  var positions = ExtractNumberPositions(948563);
  foreach (var pos in positions)
  {
   Console.WriteLine(pos);
  }
 }

 static List ExtractNumberPositions(int number)
 {
  var strNumber = number.ToString();
  var len = strNumber.Length;
  var positions = new List();
  for (int i = 0; i < len; i++)
  {
   int pos = int.Parse((strNumber[len - i - 1]).ToString()) * 
                       (int)Math.Pow(10, i);
   positions.Add(pos);
  }

  return positions;
 }
}

Give your advice to big bosses and make money

Views: 330

Tags:

C#

The non-generic method 'Microsoft.Practices.Unity.IUnityContainer.Resolve cannot be used with type arguments

by Naveen 30. June 2010 18:08

I was working on introducing Unity Framework for Silverlight in one of my older Silverlight applications. I was trying to use Resolve to get hold of singleton instance of one of my objects. I kept getting following compile time error.

error CS0308: The non-generic method 'Microsoft.Practices.Unity.IUnityContainer.Resolve(System.Type, string, params Microsoft.Practices.Unity.ResolverOverride[])' cannot be used with type arguments

There was nothing wrong with the code that I had to call Resolve method. After doing dance for more than an hour with everything I could try, I finally figured out the problem and kicked myself. I never added using declaration for Unity namespace in the class where I was using the code. After adding following line, everything was normal in my world.

using Microsoft.Practices.Unity;
Give your advice to big bosses and make money

Views: 763

Tags:

.Net | C# | Silverlight

Opensource Index.dat file viewer

by Naveen 11. June 2010 12:09

Download Installation Files

Download Source Code

What is Dat File Viewer?

Simply put, this is a very light weight application that helps you see what all secrets microsoft is hiding in index.dat files in various folders under a user's profile. As per microsoft index.dat files are their cache or index files that they create to speed up access to various web sites, applications etc. But one thing lot of people have to come realize over the time that even after you clean up your Temporary Internet Files, Cookies, History etc. files from your windows machine, these index.dat files still carry all the footprints of your internet and file activities. So analysis of these files is used as one of the forensic tools when you want to recreate a user's internet activities in the past.

I am not going to go into details on format of index.dat files and other related technical details. Following link is an excellent technical resource on inside of index.dat file. This is by FoundStone.com a devision of McAfee.

I have developed this open source application based on the original C code developed by FoundStone.com. This application is built using .Net framework.

Install It

  • Download the insaller package associated with this post.
  • Unzip this file in a folder.
  • Double click on setup.exe to launch the installer.
  • Follow the instructions and you are all set to go.

Pre-requisites

You will need to have Microsoft .Net Framework 4.0 installed on your machine to run this application. You can download the run time from the following location.

Download Microsoft .Net 4.0 Framework

I did not spend much time on the installer package to get automatic install of Microsoft .Net 4.0 framework. May be I will get to that in upcoming release. But for now, my apologies for making you do manual install of the framework if you do not already have it.

Run It

If you chose default installation option, you should have ByteBlocks Dat File Viewer entry in your start menu and you should be able to launch the applicaiton from there. If for some reason you do not see menu item in Start menu, then look under ProgramFiles/ByteBlocks folder the application. From there, you can double click on ByteBlocks.DatFileViewe.exe file to launch the application.

After you launch the application, you will see a splash screen with picture of a turtle in it. Depending on amount of data contained in your index.dat files, the application may require few seconds to load. So be little patient with load screen, the application will eventually load.

Export Results

The application allows you to export list of URLs or Coookies from following locations into a PDF file.

  • Temporary Internet Files
  • Cookies
  • History

In the top menu of the application, click on Export > PDF link to generate PDF file.

Give your advice to big bosses and make money

Views: 675

Tags: , , , , ,

.Net | .Net | C# | C# | IE | IE | Windows | Windows | Application | Application

How to get list of user accounts on machine using .Net?

by Naveen 10. June 2010 06:17

Here is a code snippet that demonstrates how to get list of user accounts on your windows machine using .Net. Since there are no native .Net APIs to accomplish this task, you will need to use Interop to use Win32 APIs related to user management.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace UserEnvironment
{
 class Program
 {
  static void Main(string[] args)
  {
   Console.WriteLine(Environment.OSVersion.VersionString);
   Console.WriteLine("OS: {0}.{1}", 
    Environment.OSVersion.Version.Major, 
    Environment.OSVersion.Version.Minor);
   Console.WriteLine("****************************************");
   var accounts = GetListOfAccounts();
   if (accounts.Count != 0)
   {
    Console.WriteLine("User accounts on " + 
     System.Environment.MachineName + System.Environment.NewLine);
    foreach (var account in accounts)
    {
     Console.WriteLine(account);
    }
   }
   else
   {
    Console.WriteLine("No user account found!");
   }
  }

  static System.Collections.Specialized.StringCollection GetListOfAccounts()
  {
   int resumeHandle;
   int entriesRead;
   int totalEntries;
   IntPtr bufPtr;

   var userAccounts = new System.Collections.Specialized.StringCollection();
   NetUserWin32.NetUserEnum(null, 0, 2, out bufPtr, -1, 
    out entriesRead, out totalEntries, out resumeHandle);
   if (entriesRead > 0)
   {
    var users = new NetUserWin32.USER_INFO_0[entriesRead];
    IntPtr iter = bufPtr;
    for (int i = 0; i < entriesRead; i++)
    {
     users[i] = (NetUserWin32.USER_INFO_0)Marshal.PtrToStructure(iter, 
                           typeof(NetUserWin32.USER_INFO_0));
     iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(NetUserWin32.USER_INFO_0)));
     userAccounts.Add(users[i].Username);
    }
    NetUserWin32.NetApiBufferFree(bufPtr);
   }
   return userAccounts;
  }
 }
}

namespace UserEnvironment
{
 public class NetUserWin32
 {
  [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
  public struct USER_INFO_0
  {
   public String Username;
  }

  [DllImport("Netapi32.dll")]
  public extern static int NetUserEnum([MarshalAs(UnmanagedType.LPWStr)]string servername, 
    int level, int filter, out IntPtr bufptr, 
    int prefmaxlen, out int entriesread, out int totalentries,
    out int resume_handle);

  [DllImport("Netapi32.dll")]
  public extern static int NetApiBufferFree(IntPtr Buffer);
	}
}

Give your advice to big bosses and make money

Views: 328

Tags: ,

.Net | C# | Win32 | Windows

How to format and modify value in data grid row at run time based on previous row values

by Viper 7. July 2009 14:35
grid view row formatting

Download Sample Project

This was a question was asked by one of my site visitors, Mike. Following is the text of the question:

This is a similar question to one you have already answered in formatting Grid Views. However there are 2 main differences. First: I want to change the value of a column (not the format) in any row if the row preceding it has a certain value in the same column.

This question translates to How do you change value of a data grid column based on values of previous row(s). I generalized this question to cover all previous rows and not just the preceding row. Answer to all such questions relies on handling events like RowDataBound or RowCreated events. When a data grid or grid view renders, RowDataBound fires when row is being data bound and then RowCreated is fired after it has been data bound and row has been created. So depending on at what stage of rendering you want to change behavior of a row, you will subscribe either of these events. In the sample project, I am subscribing to RowDataBound event.

Next step is to access values from previous rows. Here you have choice. One, you can keep some local vaiable that stores values from previous row(s) and then use them in current row event handling. Two, you can access the previous GridRow based on index. In this sample i will discuss the approach of accessing previous row based on index and then extracting values from certain cells.

In RowDataBound event handler, GridViewRowEventArgs provides you access to DataItem associated with current row only. You do not have access to DataItem associated with previous rows. But at this point, previous rows have been prepared for rendering. You have access to all the cell values associated with previous row. You can access GridRow object of previous rows and extract text from cells that you are interested in. In the sample project, I am accessing ListPrice from fourth column and then displaying it in current row along with price associated with current row. Well, this does not sound like something that is very interesting or useful. But it serves the purpose of demonstrating you will accomplish the task.

Here is the code snippet from sample project.


protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
 if (e.Row.RowType == DataControlRowType.DataRow)
 {
  double price = -1.0;
  double prevPrice = -1.0 ;
  // Access the previous row.
  if (e.Row.RowIndex != 0)
  {
   GridViewRow prevRow = this.productsGrid.Rows[e.Row.RowIndex - 1];
   if (null != prevRow && 
    prevRow.RowType == DataControlRowType.DataRow)
   {
    double.TryParse(prevRow.Cells[3].Text, out prevPrice);
   }
  }
  var thisRowData = e.Row.DataItem as DataRowView;
  if (!Convert.IsDBNull(thisRowData["ListPrice"]))
  {
   double.TryParse(thisRowData["ListPrice"].ToString(), out price);
  }
  var ctrl = e.Row.FindControl("prevPriceLabel") as Label;
  if (null != ctrl)
  {
   ctrl.Text = string.Format("{0} - {1}", prevPrice, price);
  }
 }
}

The attached sample project is a VS2010 project. There is no VS2010 or .Net4.0 implementation in the project. So if you are using VS2008 or prior, you should be able to copy the implementation files into your own project.

Feel free to send me any request for any other grid view implementation you would like to be answered or implemented.

Give your advice to big bosses and make money

Views: 2163

Tags: , ,

.Net | ASP.Net | C# | DataGrid

How to download image from a web site programatically using HttpWebRequest

by Viper 16. June 2009 19:15

I am working on adding new features to Marketweet - Twitter Autofollow application. The new feature will show some details about followers of an account. And one of the details is showing image associated with user's profile. To get that to work, I have developed an image service that downloads the user images in the background. This is done using HttpWebRequest object to send request to image URL that is set as user's profile image. Following code shows you how you can download an image from a web site programatically using HttpWebRequest. Also notice how ContentType property of HttpWebResponse is utilized to make sure that response from the specified URL is of type type/xxxx.


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace ConsoleApplication1
{
 class Program
 {
  static void Main(string[] args)
  {
   DownloadImage(113467, 
   @"http://s3.amazonaws.com/twitter_production/profile_images/214863919/logo_normal.jpg");
  }
  static void DownloadImage(int userId, string url)
  {
   HttpWebRequest webRequest = HttpWebRequest.Create(url) as HttpWebRequest;
   HttpWebResponse resp = webRequest.GetResponse() as HttpWebResponse;
   if(resp.StatusCode == HttpStatusCode.OK)
   {
    if (resp.ContentType.Contains("image/"))
    {
     int idx = resp.ContentType.IndexOf("/");
     string fileName = string.Format("{0}.{1}", 
        userId, resp.ContentType.Substring(idx + 1));
     byte[] imageContent = ProcessImageStream(resp);
     FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
     fs.Write(imageContent, 0, imageContent.Length);
     fs.Close();
    }
   }
 }
private static byte[] ProcessImageStream(HttpWebResponse resp)
{
 Int64 iContentLength = resp.ContentLength;
 byte[] streamContent;
 MemoryStream memStream = new MemoryStream();
 const int BUFFER_SIZE = 4096;
 int iRead = 0;
 int idx = 0;
 Int64 iSize = 0;
 memStream.SetLength(BUFFER_SIZE);
 try
 {
  using (memStream)
  {
   while (true)
   {
    iRead = 0;
    byte[] respBuffer = new byte[BUFFER_SIZE];
    iRead = resp.GetResponseStream().Read(respBuffer, 0, BUFFER_SIZE);
    if (iRead == 0)
    {break;}
    iSize += iRead;
    memStream.SetLength(iSize);
    memStream.Write(respBuffer, 0, iRead);
    idx += iRead;
   }
   streamContent = memStream.ToArray();
  }
 }
 catch (Exception ex)
 {
  Console.WriteLine(ex.Message);
  throw;
 }
 return streamContent;
 }
}
}

Discussion

After this entry was posted, Mark pointed out in his comment that it could be achieved in 2 lines of code. I will post those two lines of code here.


Dim r As New System.Net.WebClient  
r.DownloadFile("http://http://aspnetlibrary.com/images/logo.png", 
 "c:\aspnetlibrary.png")

This approach works perfectly fine if both variables in this method signature are very well known. What this means is that you know that target URL is what it says (that I am an image file of type PNG). Here are some issues that you have to think about when you are building a solution that involves downloading of files (image, multimedia or any kind).

  • Lets start with evil that is lurking around these days called Tiny URLs. These started with some good intent but now these have become medium to disguise spam, tricking people into clicking on links that are click advertising, redirecting to questionable sites, key loggers. So if the image URL is like http://tinyurl.com/45fghty, you don't even know what it is. So you really don't know what to save this file as.
  • If you don't know what mime type actually is associated with file that is being downloaded.

What you have to rely on is headers that are associated with response. You can see from code snippet how header information is utilized to establish file type and create file name on the fly.

Give your advice to big bosses and make money

Views: 3294

Tags:

.Net | C#

How to round off number to 0 or next 0.5

by Viper 12. June 2009 08:31

While working on a clock synchronization service, I had to dig up an old piece of code that I used to round off numbers. For this application I need to round the number to 0 or next 0.5 depending on it proximity to edge. For example if number is 8.001, it gets rounded off to 8.0 and if it is 8.6123 it will be rounded to 8.5. You can argue why not 9 instead of 8.5. Well the logic of the application demands that number is to be rounded down. Here is simple piece of code that will do the trick.


double roundDownNumber = Math.Round(actualNumber * 2.0, 0) / 2.0;

Just simple idea that multiply the number by 2 and round it to ZERO decimal places and then divide by 2.

Give your advice to big bosses and make money

Views: 1143

Tags: , ,

.Net | C#

How to group records using LINQ

by Viper 9. June 2009 04:58

While developing Twitter applications, one of the common patterns that used to emerge is that some particular users will post lot of messages in a short time span. And when searching records, I used to end up with multiple messages from one particular user. So for some user interface, I always had to group messages by user posting the message.

Following code shows how easy it is to group records using LINQ. Just to explain the code a little bit, when search is performed for a certain query term using twitter rest api with Tweetsharp, results are returned as list of TwitterSearchStatus objects. The user who posted the message is indicated by FromScreenName property. So this property becomes the key on which grouping is to be performed. Following group statement shows how grouping is done in LINQ statement.

 var resultsGrpByUser = from searchResult in searchResults
   group searchResult by searchResult.FromUserScreenName
   into userMessages
 select new { FromUser = userMessages.Key, Messages = userMessages };

Above code uses group statement to create group on user name, and the grouping results are returned as Dictionary of anonymous objects that have properties named FromUser and Messages. Following code snippet shows the whole implementation of grouping using LINQ.


static Dictionary<string, List<TwitterSearchStatus>> GetPopularTweets()
{
 List<TwitterSearchTrend> trends = GetDailyTrends();
 if (null == trends ||
     trends.Count == 0)
 {
  return null;
 }
 // For now only process first trend term.
 List<TwitterSearchStatus> searchResults = SearchForTerm(trends[0].Query);
 if (searchResults.Count == 0)
 {
  return null;
 }
 var resultsGrpByUser = from searchResult in searchResults
   group searchResult by searchResult.FromUserScreenName
   into userMessages
 select new { FromUser = userMessages.Key, Messages = userMessages };
 Dictionary<string, List<TwitterSearchStatus>> dict = 
  new Dictionary<string, List<TwitterSearchStatus>>();
 foreach(var userMessage in resultsGrpByUser)
 {
  Console.WriteLine(userMessage.FromUser);
  foreach(var twitterStatus in userMessage.Messages)
  {
   Console.WriteLine("\t{0}", twitterStatus.Text);
  }
  dict[userMessage.FromUser] = userMessage.Messages.ToList();
  Console.WriteLine("*************");
 }
 return dict;
}

Give your advice to big bosses and make money

Views: 2171

Tags:

.Net | C# | LINQ

How to verify twitter login credentials

by Viper 5. June 2009 04:53

As more and more have started to integrate Twitter API into the applications and allowing user to post messages and do other twitter related tasks, one of the essential step in the work flow is to verify a user's twitter credentials. There are certain tasks you can perform without logging into API. But when it comes to posting messages or pulling a user's data etc., Twitter API wants the user to be authenticated. Here is a code snippet that shows how you can use Twitter API to verify a user's credentials or verify user's login information.


static TwitterUser VerifyTwitterCredentials(string login, string password)
{
 IFluentTwitter ft = FluentTwitter.CreateRequest();
 ft.AuthenticateAs(login, password);
 ft.Accounts().VerifyCredentials().AsJson();
 var resp = ft.Request();
 TwitterUser tUser = resp.AsUser();
 if (null == tUser)
 {
  var err = resp.AsError();
  Console.WriteLine("Twiiter Error: " + err.ErrorMessage);
 }
 return tUser;
}

When verification fails, you will get a response that will not get converted to TwitterUser object and method will return null object.

Give your advice to big bosses and make money

Views: 3280

Tags: , ,

C# | .Net | C# | VB.Net | VB.Net

WinForms Samples, Tutorials, Demos, Articles and more

by Viper 16. April 2009 19:14

Read, download samples, tutorials etc. for WinForms.

Give your advice to big bosses and make money

Views: 1315

Tags:

.Net | C# | VB.Net | WinForms

ID3V2 Detect and Manipulate MP3 file format using C# with Idsharp Library

by Viper 21. March 2009 18:18

Long time ago I wrote an article Detect file type or mime type based on content. If you look at the XML file that contained magic signature of different file types, one of the type was MP3 files. And magic signature for MP3 files is that first 3 bytes of file content are ID3. The format of MP3 files is very well defined and explained at ID3.org. Although it is very well defined but it is not something very straight forward and not a job for faint heart to understand it.

For one of new development adventure I am tasked with digging little bit deep into file structure of MP3 files and find out some meta data. Things like Artist Name, Title, Track Number, Year Of Release etc. So I looked around the site and found reference to IDSharp library developed for .Net and is COM visible as well. So you can use it in any application that can invoke COM interfaces. The library is available on sourceforge. But there is no source code for the actual core library. It is only available as .Net assembly. I really needed source code for it because I was curious about implementation and I had to make some modifications as well for the kind of work I was doing.

So I fired up good old tool Reflector and dis-assembled the assembly to generate the source code for it. I had to fix few compile errors after disassembly. But finally I made it to work as expected. So I thought I will share the source code of IdSharp library with people who are interested in it.

Download Source Code For IdSharp Library

From the following code snippet you can see how you can load a MP3 file to get all information about the file. And then you can actually change ID3 tag information as well and save the file with new information. In the sample code, I changed the name of the artist as well as album name. You can see from the screen shot that it did work.

static void Main(string[] args)
{
var id3v2 = ID3v2Helper.CreateID3v2("Hello.mp3");
Console.WriteLine("Artist: {0}", id3v2.Artist);
Console.WriteLine("Title: {0}", id3v2.Title);
Console.WriteLine("Album: {0}", id3v2.Album);
Console.WriteLine("Genre: {0}", id3v2.Genre);
Console.WriteLine("Year: {0}", id3v2.Year);
Console.WriteLine("TrackNumber: {0}", id3v2.TrackNumber);
Console.WriteLine("Media Type: {0}", id3v2.MediaType);
Console.WriteLine("CD Identifier: {0}", id3v2.MusicCDIdentifier.TOC);
id3v2.Artist = "Byteblocks.com";
id3v2.Title = "ID3v2 Song";
id3v2.Save("Bytes.mp3");
}

Give your advice to big bosses and make money

Views: 7105

Tags: , , ,

.Net | C# | Mime Type | File Format

Manipulating Bits and Bytes Using C#

by Viper 21. March 2009 05:45

Recently I was working on detection of MP3 file format when a user uploads a file to a web site. Part of the detection was to look for ID3 header and figuring out what flags were set in header. The format specification tells you at what bit locations to check for what flags. This meant that you will need some mechanism to figure out what bits are set with in a byte. If you are seasoned C/C++ developer, you pretty much know that this meant that doing some low level bit shift operations on that byte to rotate through them to check which bit is set and which is not set. So if you will write that code in C#, it will look like snippet below.


static void ConvertToBinaryFormat(byte n)
{
 var flags = new BitArray(8);
 var counter = 8;
 var idx = 0;
 while(counter > 0)
 {
  flags.Set(idx++, ((n & 1) == 1));
  n >>= 1;
  counter--;
 }
 PrintBitArray(flags);
}

static void PrintBitArray(BitArray ba)
{
 Console.WriteLine();
 for(int i = ba.Length-1; i >= 0; i--)
 {
 Console.Write("{0}", ba[i] ? 1 : 0);
 }
}

Looking at the code, you are already saying that I have already used BitArray class object in this function. Then why am I doing all the low level operations. I intentionally introduced BitArray here to demonstrate that you really do not need to do all the low level operations to check what bits are set. BitArray class already does that for you. Following one line of code does the same thing that I did by doing some bit shifting.


var newFlags = new BitArray(new byte[] {20});
PrintBitArray(newFlags);

If you look at PrintBitArray method, you will notice that it is using reverse loop to print each bit. When bit shit operation is being done, bit array gets filled from right to left. This means that higher order bits get pushed low. And BitArray class does the same thing. So you will have to access the array from bottom.

Give your advice to big bosses and make money

Views: 3736

Tags: ,

.Net | C#

Powered by BlogEngine.NET 1.5.1.7
Theme by Naveen Kohli

By Categories