Posts Tagged ‘.net’

How to Debug Window Service without installing


Usually if we want to debug the windows service project to see if it is behaving as we expected, what we do is by installing the windows service and attaching that project to the process.

There is also another way to debug without installing it, what we need to do is by creating another project such as console project then copy and paste the code below.

  1.     Sub Main()
  2. #If Not Debug Then
  3.         Dim servicesToRun As System.ServiceProcess.ServiceBase()
  4.  
  5.         servicesToRun = New System.ServiceProcess.ServiceBase() {New MyService()}
  6.         System.ServiceProcess.ServiceBase.Run(servicesToRun)
  7. #Else
  8.         Dim service As AS2Client.AS2SenderService = New MyService()
  9.         service.myentrysub()
  10.  
  11.         System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite)
  12.  
  13. #End If
  14.  
  15.  
  16.        
  17.     End Sub


References:
Run Windows Service as a Console program

Debugging Windows Services under Visual Studio .NET

, , ,

No Comments


How to Capture form without showing it in .Net

  1. using System;
  2. using System.Drawing;
  3. using System.Runtime.InteropServices;
  4. using System.Windows.Forms;
  5.  
  6. public class ControlPainter
  7. {
  8.   private const int
  9.     WM_PRINT = 0×317, PRF_CLIENT = 4,
  10.     PRF_CHILDREN = 0×10, PRF_NON_CLIENT = 2,
  11.     COMBINED_PRINTFLAGS = PRF_CLIENT | PRF_CHILDREN | PRF_NON_CLIENT;
  12.  
  13.   [DllImport("USER32.DLL")]
  14.   private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, int lParam);
  15.  
  16.   public static void PaintControl(Graphics graphics, Control control)
  17.   { // paint control onto graphics
  18.     IntPtr hWnd = control.Handle;
  19.     IntPtr hDC = graphics.GetHdc();
  20.     SendMessage(hWnd, WM_PRINT, hDC, COMBINED_PRINTFLAGS);
  21.     graphics.ReleaseHdc(hDC);
  22.   }
  23. }
  24.  
  25. <strong>TO USE IT:
  26. </strong>
  27. using (Form f = new MyForm()) {
  28. Image i = new Image(f.Width, f.Height);
  29. Graphics g = Graphics.FromImage(i);
  30. ControlPainter.PaintControl(g, f);
  31. }

Source

Reference

,

No Comments


How to get the difference between two dates in .net

You can use the built-in function for getting the difference between two dates.

Example:

  1.         Dim d1 As DateTime = Now
  2.         Dim d2 As DateTime = Now.AddDays(3)
  3.  
  4.         Dim m As TimeSpan = d2.Subtract(d1)
  5.         Dim t As String
  6.  
  7.         t = "Days : " & m.Days.ToString() & vbCrLf
  8.         t += "Hours : " & m.Hours.ToString() & vbCrLf
  9.         t += "Minutes : " & m.Minutes.ToString() & vbCrLf
  10.         t += "Seconds : " & m.Seconds.ToString() & vbCrLf
  11.         t += "Milliseconds : " & m.Milliseconds.ToString() & vbCrLf
  12.         MessageBox.Show(t)

, ,

No Comments


sn.exe Access is denied

You can do this by running the command prompt as a administrator, just right click and run as administrator.

You can also refer to the following link if the above solution does not work:

  1. sn.exe fails with Access Denied error message
  2. How To Create an Assembly with a Strong Name in .NET Framework SDK

, ,

No Comments


Convert color to string

To convert a color to a string use the ColorConverter class but this can be access through TypeConverter class;

  1. Dim myColor as Drawing.Color = Color.Red
  2.  
  3. Dim clor As System.ComponentModel.TypeConverter = System.ComponentModel.TypeDescriptor.GetConverter(myColor)
  4. dim ColorName as string
  5.  
  6. ColorName = clor.ConvertToString(Color.Red)

, ,

No Comments


Convert color from string

To convert a color from a string use the ColorConverter class but this can be access through TypeConverter class;

  1. Dim myColor as Drawing.Color
  2.  
  3. Dim clor As System.ComponentModel.TypeConverter = System.ComponentModel.TypeDescriptor.GetConverter(GetType(Drawing.Color))
  4.  
  5. myColor = clor.ConvertFromString("Blue")

MyColor variable is  now ready to use.

, ,

No Comments


An error occurred while validating. HRESULT = ‘80004005′

An error occurred while validating. HRESULT = ‘80004005′, this error just came up when I’m trying to organize my .Net Project files in my Solution, I removed some of the projects and just use the compiled dll in referencing to the main Project. However, this error has appeared during rebuilding the setup project and it took some of my time to find the solution or the cause.

So, what I did was I removed all References from the Target Primary Output project and added them and rebuilding it again , :) that’s it no more errors.


“Thank you for reporting this issue. We were able to reproduce the problem and have identified the root cause. The problem is caused by cross-solution project reference between Solution1 and Solution2. From the attached project, the project “WindowsFormApplication1” in Solution2 references a project that is not in Solution2 (it references ClassLibrary1 from Solution1). To fix the error, the workaround is to copy the ClassLibrary1 project to Solution2 and re-add the reference to ClassLibrary1 within its own solution.


Project-to-project references only works within the same solution. If you have to split into two solutions and split the code for your class library into two projects, you need to also split the project that references the class library into two projects (one for each solution) in order to avoid project references outside the current solution.

I hope this helps.

Candy Chiang
Program Manager – Visual Studio”

For more information and explanation about this error just click here

, , ,

No Comments


Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information


I’m currently developing an application that has a plugin style or loading of DLL dynamically. So this is how I did I have created 4 projects;

  1. Plugin Interface project (Class Library)
  2. Test Plugin project (Class Library)
  3. Consumer project (Class Library) - read and execute the plugins
  4. Windows Apps that uses/references the Consumer project

When I run the #4 project I get this error “Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information“, as I’m just a beginner and can’t find a solution out of my head so I did research on the web and seen lots of solutions and recommendations but none of them works.

Then I tried to have the project referenced from #1 project and you know what it works :).

, , , ,

No Comments


Extension method in .net

Extension methods enable developers to add a method to an existing class without deriving or affecting the existing class. This extension methods only support a sub routine or a function.

Example if we want to extend the functionality of the String type

VB.NET


  1. Imports System.Runtime.CompilerServices
  2.  
  3. Module StringExtensions
  4.  
  5.     <Extension()>
  6.     Public Sub Print(ByVal aString As String)
  7.         Console.WriteLine(aString)
  8.     End Sub
  9.  
  10.     <Extension()>
  11.     Public Sub PrintAndPunctuate(ByVal aString As String,
  12.                                  ByVal punc As String)
  13.         Console.WriteLine(aString & punc)
  14.     End Sub
  15.  
  16. End Module

To use that function

  1.     Sub Main()
  2.  
  3.         Dim example As String = "Example string"
  4.         example.Print()
  5.  
  6.         example = "Hello"
  7.         example.PrintAndPunctuate(".")
  8.         example.PrintAndPunctuate("!!!!")
  9.  
  10.     End Sub

C#.NET
If you notice the parameter there is a this keyword, that is how it done.

  1. namespace ExtensionMethods
  2. {
  3.     public static class StringExtensions
  4.     {
  5.         public static int Print(<strong>this </strong>String str)
  6.         {
  7.             Console.WriteLine(String );
  8.         }
  9.     }  
  10. }
  11.  
  12. string s = "Hello Extension Method";
  13. s.Print();

, , ,

No Comments


Namespace or type specified in the project-level Imports ‘System.Xml.Linq’ doesn’t contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn’t use any aliases

There are 2 ways to remove this warning

First, you may need to convert the current .Net Framework used by the application to higher version, let’s say from .net framework 2 to 3.5

  1. Goto Property Pages of the web site project
    • View Menu->Property Pages or press shift+F4
  2. Click the Build option from the left side
    • Make sure the Target Framework is .Net Framework 3.5

Second, remove the namespace that shown in the warning in this case the “System.Xml.Linq” from the web.config

namespacelinq

namespace linq


, , , ,

No Comments



SetPageWidth