Monday, 1 July 2013

.net concepts

/* this is to find the images from the folder*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace WebApplication2
{
    public partial class imagesingrid : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string[] filePaths = Directory.GetFiles(Server.MapPath("~/Images/"));
            List<ListItem> files = new List<ListItem>();
            foreach (string filePath in filePaths)
            {
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
            }
        }

        protected void Upload(object sender, EventArgs e)
        {
            string path = txtbox.Text;
            string[] fileName = Directory.GetFiles(path);

            // List<string> str = new System.Collections.Generic.List<string>();
            foreach (string fil in fileName)
            {
                string vali = Path.GetExtension(fil).ToLower();
                if (vali == ".jpg" || vali == ".png" || vali == ".jpeg")
                {
                    string str = Path.GetFileName(fil);
                    //str.Add(Server.MapPath("~/Images/") + fileName);
                    File.Copy(fil, Server.MapPath("~/Images/") + str, true);
                }
            }

            BindImages();
            //FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Images/") + fileName);
            // Response.Redirect(Request.Url.AbsoluteUri);


        }
        private void BindImages()
        {
            string[] filePaths = Directory.GetFiles(Server.MapPath("~/Images/"));
            List<ListItem> files = new List<ListItem>();
            foreach (string filePath in filePaths)
            {
                string fileName = Path.GetFileName(filePath);
                files.Add(new ListItem(fileName, "~/Images/" + fileName));
            }
            GridView1.DataSource = files;
            GridView1.DataBind();
        }

        protected void btnUpload_Unload(object sender, EventArgs e)
        {
           
        }
    }
}


/* design*/
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="imagesingrid.aspx.cs" Inherits="WebApplication2.imagesingrid" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="txtbox" runat="server" ></asp:TextBox>
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload"
            onunload="btnUpload_Unload" />
<hr />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ShowHeader="false">
    <Columns>
        <asp:BoundField DataField="Text" />
        <asp:ImageField DataImageUrlField="Value" ControlStyle-Height="100" ControlStyle-Width="100" />
    </Columns>
</asp:GridView>
    </div>
    </form>
</body>
</html>





/* process of the system*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.ComponentModel;
using System.IO;
using System.Threading;


namespace Current_Process
{
    class Program
    {
        Process myProcess;
        static void Main(string[] args)
        {
            Program myProcess = new Program();
            myProcess.BindToRunningProcesses();
        }

        void BindToRunningProcesses()
        {
              Process currentProcess = Process.GetCurrentProcess();
            while (true)
            {
                Process[] localAll = Process.GetProcesses();
               myProcess= Process.Start("D:\\Processes.txt");

                using (StreamWriter file = new StreamWriter("D:\\Processes.txt"))
                {
                    file.Write("D:\\Processes.txt", String.Empty);
                    foreach (Process line in localAll)
                    {
                        // If the line doesn't contain the word 'Second', write the line to the file.
                        file.WriteLine(line.ToString());
                    }
                    myProcess.Refresh();
                }
                     Thread.Sleep(10000);
                     myProcess.CloseMainWindow();
                     myProcess.Close();
            }
        }
    }
}