Feb 05

.Net Properties Question asked and my Answer

C# | My Inbox 28 Comments »

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Hi,

Can any body guide me whats the exact use or *Properties* (get and set) in
C#.

I know how to declare but not sure how to use it. Please guide.

Thanks,



[KickIt] [Dzone] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Tags:

Feb 05

A Question from a reader and my Answer

C# | Design Patterns | My Inbox 4 Comments »

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Greetings!

I have a hierarchy of classes, with an abstract class that defines the
method Read() at the top of it. Each class in the hierarchy has a
Read() method, and I need to be able to call Read() from the highest
possible level of the hierarchy and get the correct Read() function.

Public class AbstractClass 
{
abstract void Read();
}

Public class A : AbstractClass
{
public override void Read();
}

Public class B: A
{
public override void Read();
}

The compiler complains that A has no overridable function named Read().
Is it not possible to do this?

Thank you very much.

RobR


[KickIt] [Dzone] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Tags:

May 26

How to retrieve data from Cookie

ASP.Net | C# 105 Comments »

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
 

Retrieve data from Cookies

 
 

In this small article I’ll explain that how to retrieve user’s data from the cookies.

In .Net 2005 we have several API’s related to Users and Roles. For example if you want to know that a User ABC belongs to a role “XYZ”, then you can proceed as follow:

User.IsInRole(“XYZ”);

The above will return you Boolean variable.

But for a small web site on which I was working, I got a point where I was interested to know the Role of the current logged in user. I was using forms authentication and was not using the User and Role API’s which are provided in .Net 2005.

The Solution is very simple. The overall summary is given below:

1.       Retrieve the cookie which you stored, during the login process (i.e. on Login page)

2.       Now decrypt that authentication ticket

3.       And now retrieve the user’s data from the ticket.

The code is given below:

string cookieName = FormsAuthentication.FormsCookieName;

1.       HttpCookie authCookie = Context.Request.Cookies[cookieName];

2.       FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);

3.       Response.Write(authTicket.UserData);

If you have assigned any role to the user, then by using authTicket.UserData you can get those roles..

Cheers,



[KickIt] [Dzone] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Tags: , ,

May 25

Filewatcher

C# | General | Tips and Tricks 119 Comments »

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
 

Working with FileSystemWatcher Class

  

Hey, if you are here to find the answer of this question "how to make sure in Created event of the Filewatcher that the file which is just created is completely copied?" then you are at the right place..

FileSystemWatcher class listens to the file system change notification and raises events when a directory, or file in a directory, changes.. In this article I’ll discuss

fileSystemWatcher1_Created(object sender, FileSystemEventArgs e)  event of FileSystemWatcher class. Working with this event is easy. You just need to capture this event and then write your code in it. BUT there is a problem in this event which is that the Created event is fired immediately as soon as the path of the file is created in the observing directory. This can be clearly explained with the help of a following scenario: I want to observe a Directory say C:\\Test. I wrote a window service which uses the FileSystemWatcher class to observe that directory. I want to pass any new file which is created in this directory to another method which processes this file. Suppose my file’s size is large (e.g. 500mb). In my window service the Created event of the FileSystemWatcher is triggered as soon as the file’s path is created in the directory, but file is not copied completely in the directory. How to solve this issue? .Net does not provide any method or event which can tell me that the file is written completely.  I tried to solve this problem and though I achieved my task, but this is not a good solution.  In the Created event I made a thread. So for each new file a new thread will be created, so we can serve each new file without any delay. In that particular thread, I tried to open the file. If file is opened successfully, then it means that file is copied completely and is available for processing. If not, then try to open the file again. Repeat this process until the file is available. Here is the code: 

void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e)

{   

       Thread _MyThread = new Thread(new ParameterizedThreadStart(this.PerformActualTask));    

       object[] Parameters = new object[] {e. FullPath };    

       _MyThread.Start(Parameters);

 

private void PerformActualTask(object parameters)

  

 //The parameter for this thread body   

 object[] Parameters = (object[])parameters;  

//Full path of the newly created file  

string _SourceFile = (string)Parameters[0];  

int WaitBeforeGiveUp = 300;  

while (true  

     

 try     

        

  //Try to open the file        

  FileStream fs = File.Open(_SourceFile, FileMode.Open);       

  {         

   //We are here because file is successfully opened         

   fs.Close();      

   //Now perform operation on that file      

   }    

 }    

 catch (Exception ex)   

       

  //If exception occured then decriment the wait before give up and ask the thread to sleep       

  if (--WaitBeforeGiveUp > 0)       

        

    // file is not available yet      

  }     

 else     

         

   //if waiting time expired.         

   // SHOW GIVE UP MESSAGE     

 }      //cause the thread to sleep     

 Thread.Sleep(1000);   

} 

}

  

In the code I made some changes which are that I tried to open a file for a particular period of time. If file opened in that period then kool, else I stored a msg in the event viewer that I have given up.     



[KickIt] [Dzone] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Tags:

May 04
 

Remove command bar from Add-ins in MS Word

 

Some time we need to develop some add-ins to fulfill our requirements in different software (e.g. MS Word). Few days back I was developing an Add-ins for MS word. The requirement was to add a button in a command bar of the MS-Word. I successfully added button in the command bar and also wrote code against the event handler of that button. But unfortunately whenever I ran the MS word, my code started to add the same button in command bar again and again. To overcome this problem I wrote code which forcefully removed that custom button from the command bar on the closing of the ms word.

The code which was removing that button on the closing of the word was in the Application_DocumentBeforeClose event of Add-in. But that code didn’t give me the expected output.

There was nothing wrong in the code, because I debugged the code and in the shutdown event I made sure that the command button exists in the command bar. If it exists then delete the button. But again that code didn’t work. And interesting thing was that the code segment which was checking that the command button exists in the command bar always gave me true and in the true section I was deleting that button, but I don’t know that why it didn’t work. I was deleting the command button by using the ID of the button something like this:

1.     Office.CommandBarPopup foundMenu = (Office.CommandBarPopup)

2.     this.Application.CommandBars.ActiveMenuBar.FindControl(Office.

3.     MsoControlType.msoControlPopup, System.Type.Missing, IDOfControl, true, true );

4.     if (foundMenu != null)

5.     {

6.       foundMenu.Delete(false);

7.     }

The above code didn’t work L 

In line 3, I used the IDOfControl to find the command bar button, but it didn’t find that control. Though I tried to use the Id.delete() method, but it also didn’t work for me. Then I changed the Line number 3 and instead of using the ID of the control to find the command bar button, I used the tag of that control and this change in code successfully find the control and this change also make the Line 6 to give the expected output.  So if you are also facing this kind of situation then try to use the tag of the button instead of its ID. I am sure it will work. Well I was developing the add-in for office 2007. This code will probably work in other version of MS word too. The reason that this code work is given below: 

Add-in Express identifies all its controls (commandbar controls) through the use of the ControlTag property (the Tag property of the CommandBarControl interface). The value of this property is generated automatically and you don’t need to change it. For your own needs, use the Tag property instead.       



[KickIt] [Dzone] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Tags: