Monday, December 13, 2010

Group By using Linq

Consider quote is a list that contain number of properties and one of the property is Item_Number , this is going to group the items by item_number property

var Items = (from e in quote
             group e by e.Item_Number into items
             select items);

Monday, December 6, 2010

Copy files using Embedded resource in C#.net

Steps:

1. Create a windows application with one button and name it as Embeded.

2. Add some existing files like image,Text file,etc..to your application by Right click on your file name which is in solution explorer and then click add - > Existing Item - > choose any file you want.

3. Then go to the properties of the files which you added then change its Build Action property to Embedded Resource.

4. Double click the Embeded button and write the following code


List<string> lstFileName = new List<string>();
            try
            {
                string s = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;

                int p = s.IndexOf("\\bin");

                string t = s.Substring(0, p);

                DirectoryInfo sourcedinfo = new DirectoryInfo(t);

                foreach (FileInfo fi in sourcedinfo.GetFiles())
                {
                    if (fi.Extension != ".cs" && fi.Extension != ".resx" && fi.Extension != ".csproj" && fi.Extension != ".user")
                    {
                        lstFileName.Add(fi.Name);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error while reading file from the path");
            }
            for (int i = 0; i < lstFileName.Count; i++)
            {
                GetResource(lstFileName[i].ToString());
            }

            MessageBox.Show("Successfully Added embeded files to Documents");


5.Then add the below function

public void GetResource(string filename)
        {

            string s = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;

            string p = Path.GetFileNameWithoutExtension(s);

            string name = p.Substring(0, p.LastIndexOf("."));

            System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();

            string val = "";

            System.IO.Stream strm = asm.GetManifestResourceStream("WindowsFormsApplication1." + filename);

            if (strm != null)
            {

                byte[] coffImage = new byte[strm.Length];

                strm.Read(coffImage, 0, coffImage.Length);

                System.IO.StreamReader reader = new System.IO.StreamReader(strm);

                val = reader.ReadToEnd();

                reader.Close();

                string addinDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\" + name;

                if (Directory.Exists(addinDirectory) == false)
                {
                    Directory.CreateDirectory(addinDirectory);
                }
                System.IO.File.WriteAllBytes(addinDirectory + @"\" + filename, coffImage);
            }

        }

6.Run your program and then click Embedded button all the embedded files will be pasted in documents.


Thanks for reading this article

Thursday, December 2, 2010

To delete recent files in Visual Studio

To Delete the recent which display left hand side of visual studio

Goto --> regedit HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\ <version >\ProjectMRUList

Friday, November 26, 2010

XAML not working properly in WPF Application

In some time the XAML wont work properly for WPF Application . To work properly run this text on run command

"%ProgramFiles%\Microsoft Visual Studio 9.0\Common7\ide\devenv" /resetSkipPkgs

Wednesday, November 17, 2010

Copy Files and Folder from one path to another path

Copying the files and folder one path and paste in some other path.


try
            {
                //check if the target directory exists
                if (Directory.Exists(target.FullName) == false)
                {
                    Directory.CreateDirectory(target.FullName);
                }

                //copy all the files into the new directory
                foreach (FileInfo fi in source.GetFiles())
                {
                    fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
                }

                //copy all the folders into the new directory
                foreach (DirectoryInfo diSourceDir in source.GetDirectories())
                {
                    DirectoryInfo nextTargetDir = target.CreateSubdirectory(diSourceDir.Name);
                    CopyAll(diSourceDir, nextTargetDir);
                }

                MessageBox.Show("Copied Successfully");
            }
            catch (IOException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }