My Tips

Tips in programming, code fragments, maybe helpfull in other projects. Its good to have those tips in one place, here .

This function check is value a valid email address:

public static bool IsEmailAddress(string email)
{   
   if(!email.Contains("@")) return false;
   string pattern = @"^(([^<>()[\]\\.,;:\s@\""]+"
                            + @"(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@"
                            + @"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
                            + @"\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+"
                            + @"[a-zA-Z]{2,}))$";
   
   Regex emailregex = new Regex(pattern);
   return emailregex.IsMatch(email);
}

Small reminder – how to get content  from url:

function LoadContents($url, $mode){
   $HTTPRequest = fopen($url, $mode);
   $contents = "";
   if ($HTTPRequest) {
   stream_set_timeout($HTTPRequest, 100);
   do {
      $data = fread($HTTPRequest, 8192);
      if (strlen($data) == 0) {
         break;
      }
      $contents .= $data;
   } while(true);
   fclose($HTTPRequest);
   }
   return $contents;
}

$contents = LoadContents("http://www.google.com", "r");

Binary read(like image, pdf, xml etc.):

$contents = LoadContents("http://www.some_url/some_chart_generator_file.php", "rb");

VB.NET class for running process/application as background thread:

Public Class Shell
   
   Private Process1 As Process = New Process()
   Sub New()
   
   End Sub
   
   Public ReadOnly Property Active() As Boolean
      Get
         Return Process1.HasExited
      End Get
   End Property
   
   Public Sub Run(ByVal sProcessPath As String, ByVal sArguments As String)
      Process1.StartInfo.FileName = sProcessPath
      Process1.StartInfo.WindowStyle = ProcessWindowStyle.Minimized
      Process1.StartInfo.CreateNoWindow = False
      Process1.StartInfo.UseShellExecute = True
      Process1.EnableRaisingEvents = True
      Process1.StartInfo.Arguments = sArguments
      Try
         'Start Process
         Process1.Start()
         Wait()
      Catch ex As Exception
         MsgBox(ex.Message)
      Finally
         Process1.Close()
      End Try
   End Sub
   
   Private Sub Wait()
      If Not Process1.HasExited Then
         System.Threading.Thread.Sleep(1000)
      End If
   End Sub
End Class

Example for transparent printing txt file:

Dim sproc As New Shell
sproc.Run("notepad", " /p c:\test\example.txt" )

Example for transparent printing pdf  file with FoxitReader :

Dim pdfAppPath as String = String.Empty
Dim progamsfolder As String = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
If File.Exists(progamsfolder & "\Foxit\FoxitReader.exe") Then pdfAppPath = progamsfolder & "\Foxit\FoxitReader.exe"
   
Dim sproc As New Shell
sproc.Run(pdfAppPath, " /p c:\test\example.pdf")