30 Aralık 2010 Perşembe

Tckimlik WebService Validate

Biraz geç oldu ama inşallah bunada para istemezler artık...

Şimdi;Windows application projesi oluşturuyoruz form'a 5 adet textbox,bir buton koyuyoruz.(label ları gerekli textboxların yerine siz koyarsınız)

Daha sonra projeye service reference dan http://tckimlik.nvi.gov.tr/Service/KPSPublic.asmx bu web servisini projemize ekleyip kodlama tarafına (cs tarafına) using TcKimlik.Dogrulama; satırını ekliyoruz.
Daha sonra eklemiş olduğumuz bir tane butonun üzerine çift tıklayıp şunları yazıyoruz.

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                bool sonuc;
                TcKimlik.Dogrulama.KPSPublicSoapClient tcKimlik = new KPSPublicSoapClient();
                sonuc = tcKimlik.TCKimlikNoDogrula(txtTcKimlik.Text, txtAd, txtSoyad, txtDogumTarihi.toString());
                if (sonuc == true)
                {
                    txtSonuc.Text = "Olumlu";
                }
                else
                {
                    txtSonuc.Text = "Olumsuz";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

Log Message to a file

LogMessageToFile ("Mesaj Log durumu"); 
 
using System; 
using System.IO; 
 
public string GetTempPath()
{
   string path = 
      System.Environment.GetEnvironmentVariable ("TEMP");
   if (!path.EndsWith ("\\"))
   {
      path += "\\";
   }
   return path;
}
 
public void LogMessageToFile (string message)
{
   System.IO.StreamWriter sw = 
      System.IO.File.AppendText(
         GetTempPath() + "Logfile.txt"); // Dosya adının değişikliği
   try
   {
      string logLine = 
         System.String.Format(
            "{0:G}: {1}.", System.DateTime.Now, message);
      sw.WriteLine(logLine);
   }
   finally
   {
      sw.Close();
   }
}

Convert Image File To MimeType

Bu Code Snippet'ın işimize yarayacağına eminim çünkü herhangi bir component yada dll kullanmadan convert edebilmemizi sağlıyor..
public static byte[] ConvertImage(byte[] fromImage, string mimeType)
  
{
    // Read the image from the byte variable into a bitmap variable
    MemoryStream fromImageStream = new MemoryStream();
    fromImageStream.Write(fromImage, 0, fromImage.Length);
    Image image = Image.FromStream( fromImageStream, true ) ;
    Bitmap bitmap = (Bitmap) image;
    // Instantiate the encoder
    EncoderParameters encoderParams = new EncoderParameters();
    encoderParams.Param[0] = new EncoderParameter( Encoder.Quality, 50L );
    ImageCodecInfo codecInfo = GetEncoderInfo( mimeType );
    MemoryStream newImage = new MemoryStream();
    // Convert the image to the new format
    bitmap.Save( newImage, codecInfo, encoderParams );
    // Read the new image into a byte variable
    byte[] data = newImage.ToArray();
    return data;
}

private static ImageCodecInfo GetEncoderInfo(String mimeType)
  {
   int j;
   ImageCodecInfo[] encoders;
   encoders = ImageCodecInfo.GetImageEncoders();
   for(j = 0; j < encoders.Length; ++j)
   {
    if(encoders[j].MimeType == mimeType)
     return encoders[j];
   }
   return null;
  }

23 Aralık 2010 Perşembe

Date Formatting in C#

<%= String.Format("{specifier}", DateTime.Now) %>

Specifier Description Output
d           Short Date                    08/04/2007
D           Long Date                    08 April 2007
t           Short Time                    21:08
T           Long Time                    21:08:59
f           Full date and time                    08 April 2007 21:08
F           Full date and time (long)                    08 April 2007 21:08:59
g           Default date and time                    08/04/2007 21:08
G           Default date and time (long)                    08/04/2007 21:08:59
M           Day / Month                    08 April
r           RFC1123 date                    Sun, 08 Apr 2007 21:08:59 GMT
s           Sortable date/time                    2007-04-08T21:08:59
u           Universal time, local timezone                    2007-04-08 21:08:59Z
Y           Month / Year                    April 2007
dd           Day                    08
ddd           Short Day Name                    Sun
dddd           Full Day Name                    Sunday
hh           2 digit hour                    09
HH           2 digit hour (24 hour)                    21
mm           2 digit minute                    08
MM           Month                    04
MMM           Short Month name                    Apr
MMMM           Month name                    April
ss           seconds                    59
tt           AM/PM                    PM
yy           2 digit year                    07
yyyy           4 digit year                    2007
:           seperator, e.g. {0:hh:mm:ss}                    09:08:59
/           seperator, e.g. {0:dd/MM/yyyy}                    08/04/2007

Find and Replace Text in Text / HTML Files

public void ReplaceInFile(string filePath,string replaceText,string findText)
{
    try
    {
         System.IO.StreamReader objReader;
         objReader= new System.IO.StreamReader(filePath);
         string content= objReader.ReadToEnd();
         objReader.Close();

         content= Regex.Replace(content,findText,replaceText);

         StreamWriter writer= new StreamWriter(filePath);
         writer.Write(content);
         writer.Close();
    }

     catch(IOException iexp)
{
   throw;
}

12 Kasım 2010 Cuma

Javascript'de Captcha Kod Yaratma...

İlgimi çeken bir konu olmuştur her zaman javascript te olması da ayrı bir zevk açıkcası.

Öncelikle Bir Asp.Net Web Site oluşturuyoruz. Default.aspx e yazılacak kod bölümü;


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 id="Head1" runat="server">
    <title></title>
     <script language="javascript">
         var a = 49, b = 65;
         var c = 100;
         var d = 70;
         function show() {
             if (a == 57) {
                 a = 49;
             }
             var main = document.getElementById('txt1');
             var a1 = String.fromCharCode(a);
             var b1 = String.fromCharCode(b);
             var c1 = String.fromCharCode(c);
             var d1 = String.fromCharCode(d);
             main.value = a1 + b1 + c1 + d1;
             a = a + 1;
             b = b + 1;
             c = c + 1;
             d = d + 1;
         }
  </script>
    <style type="text/css">
        #form1
        {
            height: 95px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input type="text" id="txt1" runat="server"
            style="border-style: none; border-color: inherit; border-width: medium; background-color:black; color:red; font-family: 'Curlz MT'; font-size: x-large; font-weight: bold; font-variant: normal; letter-spacing: 10pt; width: 120px; background-image: url('glitter_background_b4.gif');"
            value="4e5yt" />
  <input type="button" onclick="show()" value="Change"  />
        </div>
    <asp:TextBox ID="txtverification" runat="server"></asp:TextBox>
&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Button ID="Button1" runat="server" Text="Verification"
        onclick="Button1_Click" />
    &nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Label ID="lblmsg" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label>
    </form>
</body>
</html>
Cs tarafında yazılacak kodlar;
protected void Button1_Click(object sender, EventArgs e)
{
    if (txtverification.Text == txt1.Value)
    {
        lblmsg.Text = "Successful";
    }
    else
    {
        lblmsg.Text = "Failure";
    }
}

9 Kasım 2010 Salı

Word Formatını html,rtf,txt,xml formatlarına çeviren Class

Aklıma geldi nasıl olur diye bir class yazayım dedim küçük bir araştırma ile şöyle birşey yazdım.

Öncelikle Class Library Yaratıyoruz
Sonra Referans ekleme işlemlerinden COM tabına gelip
Oradan Microsoft.Office.Interop.Word Component ını seçiyoruz.


public void WordFileTo(string originalFile, object SavePath, string SaveFile, string FileTo)
{
  try
  {
    if (SaveFile == "")
    {
         return;
    }
 
    if (originalFile.Trim() != "" && SavePath.ToString().Trim() != "")
    {
      Microsoft.Office.Interop.Word.ApplicationClass WordApp = new
            Microsoft.Office.Interop.Word.ApplicationClass();
      Microsoft.Office.Interop.Word.Document WordDoc = new
Microsoft.Office.Interop.Word.Document();
      object DocNoParam = Type.Missing;
      object DocFileName = originalFile;
      object DocReadOnly = false;
      object DocVisible = false;
      object SaveToFormat = "";
 
      // Dosya yolunu belirttikten sonra word dökümanımızı açıyoruz
      WordDoc = WordApp.Documents.Open(ref DocFileName, ref DocNoParam, ref DocReadOnly,
ref DocNoParam, ref DocNoParam, ref DocNoParam, ref DocNoParam,
ref DocNoParam, ref DocNoParam, ref DocNoParam, ref DocVisible,
ref DocNoParam, ref DocNoParam, ref DocNoParam, ref DocNoParam,
ref DocNoParam);
      WordDoc.Activate();
 
      if (FileTo == "html")
      {
         SaveToFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;
         SavePath = SavePath + SaveFile + ".html";
      }
      if (FileTo == "rtf")
      {
         SaveToFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatRTF;
         SavePath = SavePath + SaveFile + ".rtf";
      }
      if (FileTo == "txt")
      {
         SaveToFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatText;
         SavePath = SavePath + SaveFile + ".txt";
              }
      if (FileTo == "xml")
      {
         SaveToFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXML;
         SavePath = SavePath + SaveFile + ".xml";
      }
     
//Belirtilen yol ve formata göre dosyayı kayıt ediyoruz.
      WordDoc.SaveAs(ref SavePath, ref SaveToFormat, ref DocNoParam, ref DocNoParam, ref
                  DocNoParam, ref DocNoParam, ref DocNoParam, ref DocNoParam, ref
DocNoParam, ref DocNoParam, ref DocNoParam, ref DocNoParam, ref DocNoParam, ref DocNoParam, ref DocNoParam, ref DocNoParam);
 
 
      // Orjinal dosyaya herhangi bir değişiklik yapmak istemiyorsak
     object SaveChanges = false;
      WordApp.Quit(ref SaveChanges, ref DocNoParam, ref DocNoParam);
    }
    else
    { return; }
  }
  catch (Exception)
  {
      return;
  }
}