GeoAvila (Geovanny G. Avila) Desarrollo.. de software.

14Aug/090

Listar Servicios de Windows con .Net

Bueno una función que nos puede ayudar en determinado momento es listar los servicios activos de Windows un ejemplo sencillo de como hacerlo es hacer usando System.Serviceprocess (NO olvides agregarlo como referencia al proyecto):

VB.NET:
  1. Imports System
  2. Imports System.ServiceProcess
  3.  
  4. Class Program
  5. Private Shared Sub Main()
  6. Dim services As ServiceController() = ServiceController.GetServices()
  7.  
  8. Console.WriteLine("Listado de los servicios corriendo : ")
  9. For Each service As ServiceController In services
  10. If service.Status = ServiceControllerStatus.Running Then 'cambiar por ServiceControllerStatus.Stopped para ver inactivos
  11. Console.WriteLine()
  12. Console.WriteLine(" Nombre de servicio: {0}", service.ServiceName)
  13. Console.WriteLine(" Nombre mostrado: {0}", service.DisplayName)
  14. End If
  15. Next
  16. End Sub
  17. End Class

y en c# sería así.

C#:
  1. using System;
  2. using System.ServiceProcess;
  3.  
  4. class Program
  5. {
  6. static void Main()
  7. {
  8. ServiceController[] services = ServiceController.GetServices();
  9.  
  10. Console.WriteLine("Listado de los servicios corriendo : ");
  11. foreach (ServiceController service in services)
  12. {
  13. if (service.Status == ServiceControllerStatus.Running) //cambiar por ServiceControllerStatus.Stopped para ver inactivos
  14. {
  15. Console.WriteLine();
  16. Console.WriteLine(" Nombre de servicio: {0}",
  17. service.ServiceName);
  18. Console.WriteLine(" Nombre mostrado: {0}",
  19. service.DisplayName);
  20. }
  21. }
  22. }
  23. }

para saber los procesos que están inactivos usamos la siguiente propiedad.
ServiceControllerStatus.Stopped.

un buen truco espero les sirva.

11Aug/090

Impedir que un form se cierre csharp,vb.net

A continuación, este código te ayudará para evitar que un formularion se cierre sin previa validación, para esto usamos la evento FormClosing de los formulacion de Windows Forms.

En Visual Basic .Net es de esta manera

VB.NET:
  1. Private Sub forma_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
  2. Dim valor1 As [Decimal]
  3. Dim valor2 As [Decimal]
  4. valor1 = Convert.ToDecimal(txtval1.Text)
  5. valor2 = Convert.ToDecimal(txtval2.Text)
  6. If valor1 <> valor2 Then
  7. MessageBox.Show(Me, "Los valore deben coincidir", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
  8. e.Cancel = True 'evita que el form se cierre
  9.  
  10. End If
  11. End Sub

C#:
  1. private void forma_FormClosing(object sender, FormClosingEventArgs e)
  2. {
  3. Decimal valor1;
  4. Decimal valor2;
  5. valor1 = Convert.ToDecimal(valor1.Text);
  6. valor2 = Convert.ToDecimal(valor2.Text);
  7. if (valor1 != valor2)
  8. {
  9. MessageBox.Show(this, "Los valores deben coincidir", "Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
  10. e.Cancel = true; //evita que el form se cierre
  11. }
  12.  
  13. }

Un código bastante útil en el tema de validaciones y prohibiciones para que esas ocaciones que se necesita aplicar seguridad en los formularios.

Nos vemos.

6Aug/090

Valida TextBox si es Entero o Decimal

Bueno estas funciones ayudan a validar si el texto ingresado en un textbox es un valor numérico ó es un valor decimal, estas son de las validaciones más usadas para desarrollar así que acá se los dejo.

VB.NET:
  1. Public Shared Function EsInteger(ByVal theValue As String) As Boolean
  2.     'funcion para enteros
  3.     Try
  4.         Convert.ToInt32(theValue)
  5.         Return True
  6.     Catch
  7.         Return False
  8.     End Try
  9. End Function
  10.  
  11. Public Shared Function EsDecimal(ByVal theValue As String) As Boolean
  12.     'funcion para enteros
  13.     Try
  14.         Convert.ToDecimal(theValue)
  15.         Return True
  16.     Catch
  17.         Return False
  18.     End Try
  19. End Function

C#:
  1. public static bool EsInteger(string theValue)//funcion para enteros
  2.         {
  3.             try
  4.             {
  5.                 Convert.ToInt32(theValue);
  6.                 return true;
  7.             }
  8.             catch
  9.             {
  10.                 return false;
  11.             }
  12.         }
  13.  
  14.         public static bool EsDecimal(string theValue)//funcion para enteros
  15.         {
  16.             try
  17.             {
  18.                 Convert.ToDecimal(theValue);
  19.                 return true;
  20.             }
  21.             catch
  22.             {
  23.                 return false;
  24.             }
  25.         }

otra manera de validar de parte de @cmsalvado gracias por el dato:

VB.NET:
  1. Public Shared Function EsInteger(ByVal theValue As String) As Boolean
  2. Dim value As Integer
  3. Return Integer.TryParse(theValue, value)
  4. End Function

C#:
  1. public static bool EsInteger(string theValue)
  2. {
  3. int value;
  4. return int.TryParse(theValue, out value);
  5. }

bueno espero les sea de gran ayuda.

nos vemos

Sigueme en Twitter

Recent Posts

Amigos de Twitter

_Noctua_
jeanfer
roger213tm
dic7
miguelin
YuryBlack
lolplaying
carlosakita
elQuique
freddier
jmangt
jepser
guatemalajoven
vochomaster
paolamurias
espyder
rodrigopolo
SiRGt
abasme
toigt
demuxer
Ronald_MacKay
spam
donttrythis
Calizman
ykro
agdsys
damarist
BcB
katiuskaflores
mestradaa
S2RD2
tutuista
pfpaau
jesmyc
promeme
Friends: 120 Followers: 206

Categories

Sitios amigos

Algunos Derechos Reservados

Blog bajo licencia Creative Commons Attribution-ShareAlike 3.0 License
Creative Commons License