Search blog.co.uk

Posts archive for: August, 2007
  • Copy To ClipBoard Functionality

    Hi,

    Hereby I am going to present copy to clipboard functionality using C#.
    This is very simple coding.

    string sCopytoClipboard = "The string which you want to copy to clip board";
    Clipboard.SetDataObject(sCopytoClipboard , true);
    IDataObject iData = Clipboard.GetDataObject();
    IDataObject data = Clipboard.GetDataObject();
    if (data.GetDataPresent(typeof(string))) If Clipboard has data then this will go into the loop
    {
        string strData = (string)data.GetData(typeof(string));
    StrData will be the copied data into the clipboard
    }

     This part of code you have to call inside the event which you are trying to copy into clipboard like "Ctrl+C" or "Ctrl+X"

    Regards
    Ravi.Battula

  • Notepad search functionality

    Few months back, I had a problem of coding Find functionality in C#. I should work like notepad Find dailog box.

    I used SetForegroundWindow(...), Actually this is not dialogbox, also this is not seperate window. Perhaps this is called from main window as objSubForm.Show(this).

    the Idea I got from
    http://www.codeguru.com/forum/showthread.php?t=417099&highlight=Find+Functionality+Notepad
    Hereby I am presenting sample code behind this.

          1.   Create windows application, add one more new  form to this project.
          2.   Declare one emplty object for second from.
                            Test.Form2 frm; // Test is namespace
          3.   In the constructor of main from create instance for second from like
                           ex. frm = new Test.Form2(this);  
          4.  In the second from you need to define on perameterised constructor and the parameter should be the object of main form 
          5. The second form code will be like

    using System.Runtime.InteropServices;

    namespace Test
    {
        public partial class Form2 : Form
        {
            Test.Form1 m_frm=null;
            [DllImport("user32.dll")] private static extern
                bool SetForegroundWindow(IntPtr hWnd);
           
            public Form2(Test.Form1 frm)
            {
                m_frm = frm;
                InitializeComponent();
            }
        }
    }

           6. In main from Find event the code should be

    frm.Show(this);

  • WCF Impersonation behavior

    Today I found how to do settings for impersonation and configuration settings for impersonation.
    This will be usuaful for the developers who are working on WCF.
    Here I will expalin with the code

    Server Code

    In this I have two methods
    One to create a text file, at the same time it will add some message ti tat text file
    Other method is to read string from that text file.

    In client Application I am calling these two methods.

    [OperationBehavior(Impersonation = ImpersonationOption.Allowed)]
    public void WriteToFile(string sText) This will create a text file and add sText data to that file.
    {
    FileInfo temp = new FileInfo("Hello.txt");
    StreamWriter text = temp.CreateText();
    text.WriteLine(sText);
    text.Close();
    Console.WriteLine(sText);
    }

    If you don't use ImpersonationOption.Allowed, It will give impersonation exception.
    If you are not dealing with the files you don't need to add impersonation property.

    [OperationBehavior(Impersonation = ImpersonationOption.Allowed)]
    public string ReadFromFile()
    This method will read data from Hello.Txt file and return that string
    {
    Console.WriteLine("End of Writing");
    StreamReader read = File.OpenText("Hello.txt");
    string sRead = null;
    sRead = read.ReadLine();
    if (sRead == null)
    return "Battula";
    else
    return sRead + " Battula";

    }

    configuration files contains normal client server configuration.
    I used netTcpBinding binding.

    server configuration file

    <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior name="MetadataBehavior">
              <serviceThrottling maxConcurrentCalls="1" maxConcurrentSessions="1" maxConcurrentInstances="1"/>
              <serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:6010/clsFiles1/" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <services>
          <service behaviorConfiguration="MetadataBehavior" name="Files1.clsFiles1">
            <endpoint address="" binding="netTcpBinding" bindingConfiguration=""
             name="battula" contract="Files1.IFiles1" />
            <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
             name="ep2" contract="IMetadataExchange" />
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:6010/clsFiles1/" />
                <add baseAddress="net.tcp://localhost:6011/clsFiles1/" />
              </baseAddresses>
            </host>
          </service>
        </services>
      </system.serviceModel>

    The Client Configuration file I used

     <system.serviceModel>
        <bindings>
          <netTcpBinding>
            <binding name="battula" closeTimeout="00:01:00" openTimeout="00:01:00"
                receiveTimeout="00:10:00" sendTimeout="00:01:00" transactionFlow="false"
                transferMode="Buffered" transactionProtocol="OleTransactions"
                hostNameComparisonMode="StrongWildcard" listenBacklog="10"
                maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10"
                maxReceivedMessageSize="65536">
              <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                  maxBytesPerRead="4096" maxNameTableCharCount="16384" />
              <reliableSession ordered="true" inactivityTimeout="00:10:00"
                  enabled="false" />
              <security mode="Transport">
                <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
                <message clientCredentialType="Windows" />
              </security>
            </binding>
          </netTcpBinding>
        </bindings>
        <client>
          <endpoint address="net.tcp://localhost:6011/clsFiles1/"
              binding="netTcpBinding" bindingConfiguration="battula" contract="IFiles1"
              name="battula">
            <identity>
              <userPrincipalName value="BBO-XP\bbo" />
            </identity>
          </endpoint>
        </client>
      </system.serviceModel>

      

    The client apllication what I tested is very simple and basic one. I have two command buttions and two text boxes. One command buttion is create a text file and it can write some content into that text file, that content we have to write in text box.

    The other command button read data from that text file an write that content into the other text box.

    Code

    namespace FilesClient
    {
        public partial class Form1 : Form
        {
            Files1Client m_client = new Files1Client();
            public Form1()
            {
                InitializeComponent();
            }

            private void cmdWrite_Click(object sender, EventArgs e)
            {
                string str = txtWrite.Text;
                m_client.WriteToFile(str);
            }

            private void cmdRead_Click(object sender, EventArgs e)
            {
                string str =m_client.ReadFromFile();
                txtRead.Text = str;
            }
        }
    }

    Regards
    Ravi.Battula

Footer:

The content of this website belongs to a private person, blog.co.uk is not responsible for the content of this website.