1
Seguridad Informática / Seguridad / Me aparece como una persona en el equipo
en: 20 Abril 2024, 17:28 pm
Hola. Windows 10 me ocurre cosas raras, me aparece e outlook del email ese, todo el rato ejecutándose una y otra vez. No me deja ni ver ni ver, ni escribir por aquí, debo borrar todo el rato el proceso porque si no, se me acaba agotando la RAM y Windows se vuelve muy lento. Al intentar reiniciar el P o apagarlo me dice este mensaje.Otra persona está todavía usando este equipo. Si lo apagas ahora, esa persona podría perder el trabajo que no haya guardado. Ni con antivirus ni nada. ¿Alguna idea? Al menos quiero saber el proceso que me ejecuta el outlook. Saludos.
2
Programación / .NET (C#, VB.NET, ASP) / Re: Mejor guardado para XML
en: 10 Marzo 2024, 04:06 am
Muchas gracias a los dos, muy buena info.
Ya que estamos, lo he hecho con archivos de txt en el cual a nadie le interesa pero funciona.
namespace Guardar_y_cargar_datos_en_archivo. txt_ . NET_7 . _0
{
public partial class Form1 : Form
{
public Form1( )
{
InitializeComponent( ) ;
}
private void Form1_Load( object sender, EventArgs e)
{
CargarControles( ) ;
}
void GuardarCrontoles( )
{
string archivoDeTexto = "" ;
int numero = 0 ;
foreach ( Control control in this . Controls )
{
continue ;
else if ( control
is TextBox
) archivoDeTexto += ( numero++ ) . ToString ( ) + ": " + ( ( TextBox) control) . Text + "\n " ;
else if ( control
is CheckBox
) archivoDeTexto
+= ( numero
++ ) . ToString ( ) + ": " + ( ( CheckBox
) control
) . Checked + "\n " ; else if ( control
is RadioButton
) archivoDeTexto
+= ( numero
++ ) . ToString ( ) + ": " + ( ( RadioButton
) control
) . Checked + "\n " ; else if ( control
is GroupBox
) foreach ( Control ctrl in ( ( GroupBox) control) . Controls )
archivoDeTexto += ( numero++ ) . ToString ( ) + ": " + ( ( TextBox) ctrl) . Text + "\n " ;
else if ( ctrl
is CheckBox
) archivoDeTexto += ( numero++ ) . ToString ( ) + ": " + ( ( CheckBox) ctrl) . Text + "\n " ;
else if ( ctrl
is RadioButton
) archivoDeTexto += ( numero++ ) . ToString ( ) + ": " + ( ( RadioButton) ctrl) . Text + "\n " ;
}
// Guardar archivo de texto en Controles.txt.
File. WriteAllText ( "Controles.txt" , archivoDeTexto) ;
}
void CargarControles( )
{
int numero = 0 ;
string [ ] lineas;
if ( File. Exists ( "Controles.txt" ) )
lineas = File. ReadAllLines ( "Controles.txt" ) ;
else
return ;
foreach ( Control control in this . Controls )
{
continue ;
else if ( control
is TextBox
) ( ( TextBox) control) . Text = lineas[ numero++ ] . Substring ( 3 + ( numero >= 10 ? 1 : 0 ) ) ;
else if ( control
is CheckBox
) ( ( CheckBox
) control
) . Checked = lineas
[ numero
++ ] . Substring ( 3 + ( numero
>= 10 ? 1 : 0 ) ) == "True" ? true : false ; else if ( control
is RadioButton
) ( ( RadioButton
) control
) . Checked = lineas
[ numero
++ ] . Substring ( 3 + ( numero
>= 10 ? 1 : 0 ) ) == "True" ? true : false ; else if ( control
is GroupBox
) foreach ( Control ctrl in ( ( GroupBox) control) . Controls )
( ( TextBox) ctrl) . Text = lineas[ numero++ ] . Substring ( 3 + ( numero >= 10 ? 1 : 0 ) ) ;
else if ( ctrl
is CheckBox
) ( ( CheckBox
) ctrl
) . Checked = lineas
[ numero
++ ] . Substring ( 3 + ( numero
>= 10 ? 1 : 0 ) ) == "True" ? true : false ; else if ( ctrl
is RadioButton
) ( ( RadioButton
) ctrl
) . Checked = lineas
[ numero
++ ] . Substring ( 3 + ( numero
>= 10 ? 1 : 0 ) ) == "True" ? true : false ; }
}
private void button_Guardar_Click( object sender, EventArgs e)
{
GuardarCrontoles( ) ;
}
}
}
Y como dice arriba el compañero. ¿Por qué no json?
Pues también funciona.
using System.Text.Json ;
namespace Guardar_y_cargar_datos_en_archivo. json_ . NET_7 . _0___02
{
public partial class Form1 : Form
{
public Form1( )
{
InitializeComponent( ) ;
}
class MiInfo
{
public string ? TextBox { get; set; }
public bool CheckBox { get; set; }
public bool RadioButton1_Luz { get; set; }
public bool RadioButton2_Luz { get; set; }
}
// Guardar datos en el archivo Ejemplo.json.
void GuardarDatos( )
{
{
TextBox = textBox1. Text ,
RadioButton1_Luz
= radioButton1
. Checked ,
RadioButton2_Luz
= radioButton2
. Checked } ;
// Guardar con espacios en blanco el archivo .json.
var opciones
= new JsonSerializerOptions
{ WriteIndented
= true } ; string serializado = JsonSerializer. Serialize ( miInfo, opciones) ;
// Crear archivo Ejemplo.json.
File. WriteAllText ( @"Ejemplo.json" , serializado) ;
}
// Cargar datos en el archivo Ejemplo.json.
void CargarDatos( )
{
// ¿Existe el archivo Ejemplo.json?
// if (File.Exists(@"Ejemplo.json") == true)
if ( File. Exists ( @"Ejemplo.json" ) )
{
// Sí. Lee el archivo completo.
string serializado = File. ReadAllText ( @"Ejemplo.json" ) ;
MiInfo? miInfo = JsonSerializer. Deserialize < MiInfo> ( serializado) ;
// ¿Es distinto a nulo?
if ( miInfo != null )
{
// Sí. Lee los datos del archivo Ejemplo.json y los carga
// en sus componentes indicados.
textBox1. Text = miInfo. TextBox ;
checkBox1
. Checked = miInfo
. CheckBox ; radioButton1
. Checked = miInfo
. RadioButton1_Luz ; radioButton2
. Checked = miInfo
. RadioButton2_Luz ; }
}
}
private void button_Guardar_Click( object sender, EventArgs e)
{
GuardarDatos( ) ;
}
private void Form1_Load( object sender, EventArgs e)
{
CargarDatos( ) ;
}
}
}
Contenido del json.
{
"TextBox": "Hola amigo.",
"CheckBox": false,
"RadioButton1_Luz": false,
"RadioButton2_Luz": true
}
Salu2.
3
Programación / .NET (C#, VB.NET, ASP) / Mejor guardado para XML
en: 9 Marzo 2024, 22:53 pm
Tengo este código para guardar y recuperar datos en XML.
Siempre me dicen que use getters y setters.
La forma que me enseñaron es antigua y nada que ver con ahora.
Por ejemplo.
using System.Xml ;
namespace Guardar_y_cargar_datos_en_archivo. xml_ . NET_7 . _0
{
public partial class Form1 : Form
{
public Form1( )
{
InitializeComponent( ) ;
}
// Guarda los datos introducidos a parte de crear el archivo "miXML.xml".
private void button_Guardar_Click( object sender, EventArgs e)
{
XmlWriter w = XmlWriter. Create ( @"archivo.xml" ) ;
w. WriteStartElement ( "MiInfo" ) ;
w. WriteElementString ( "TextBox" , textBox1. Text ) ;
w
. WriteElementString ( "CheckBox" , checkBox1
. Checked . ToString ( ) ) ; w
. WriteElementString ( "radioButton1_Luz_ON" , radioButton1
. Checked . ToString ( ) ) ; w
. WriteElementString ( "radioButton2_Luz_OFF" , radioButton2
. Checked . ToString ( ) ) ; w. WriteEndElement ( ) ;
w. Close ( ) ;
}
private void Form1_Load( object sender, EventArgs e)
{
try
{
XmlReader r = XmlReader. Create ( "archivo.xml" ) ;
r. ReadStartElement ( "MiInfo" ) ;
textBox1. Text = r. ReadElementContentAsString ( ) ;
checkBox1
. Checked = Convert
. ToBoolean ( r
. ReadElementContentAsString ( ) ) ; radioButton1
. Checked = Convert
. ToBoolean ( r
. ReadElementContentAsString ( ) ) ; radioButton2
. Checked = Convert
. ToBoolean ( r
. ReadElementContentAsString ( ) ) ; r. ReadEndElement ( ) ;
r. Close ( ) ;
}
catch ( Exception)
{
//
}
}
}
}
Por supuesto, me funciona como me han enseñado.
El archivo es este que se queda guardado.
<?xml version ="1.0" encoding ="utf-8" ?>
<MiInfo>
<TextBox> Hola amigo.</TextBox>
<CheckBox> True</CheckBox>
<radioButton1_Luz_ON> False</radioButton1_Luz_ON>
<radioButton2_Luz_OFF> True</radioButton2_Luz_OFF>
</MiInfo>
Entonces.
¿Cuál es la mejor manera de programarlo?
Saludos.
7
Programación / .NET (C#, VB.NET, ASP) / ¿Alguna manera de enviar mensajes desde C# a WhatsApp?
en: 31 Diciembre 2023, 04:31 am
Buenas y felices fiestas 2023.
Siguiendo este vídeo, a mi no me funciona, ya que indica bajarse el WhatsApp desde Microsoft Store y no encuentro el ejecutable por algún lado.
VIDEO El código es este:
using System ;
using System.Diagnostics ;
using System.Threading ;
using System.Windows.Forms ;
namespace WhatsApp_01
{
public partial class Form1 : Form
{
public Form1( )
{
InitializeComponent( ) ;
}
private void button_Enviar_Click( object sender, EventArgs e)
{
string numero = textBox_Numero. Text ;
string mensaje = richTextBox_Mensaje. Text ;
// Abrir la aplicación de WhatsApp versión escritorio.
Process proceso
= new Process
( ) ; proceso. StartInfo . FileName = @"C:\users\usuario\AppData\WhatsApp\WhatsApp.exe" ;
proceso. Start ( ) ;
Thread. Sleep ( 5000 ) ;
SendKeys. SendWait ( "^n" ) ;
Thread. Sleep ( 2000 ) ;
SendKeys. SendWait ( numero) ;
SendKeys. SendWait ( "{Enter}" ) ;
Thread. Sleep ( 2000 ) ;
SendKeys. SendWait ( mensaje) ;
SendKeys. SendWait ( "{Enter}" ) ;
}
private void textBox_Numero_KeyPress( object sender, KeyPressEventArgs e)
{
if ( ! char . IsControl ( e. KeyChar ) && ! char . IsDigit ( e. KeyChar ) )
{
e. Handled = true ;
}
}
}
}
En mi caso desde WhatsApp, también puede ser Telegram, enviar mensajes como comandos, lo lea el PC y activo Led porque tiene incorporado Arduino. Lo que si puedo hacer es enviar comandos desde gmail y hotmail en modo consola y Windows Form con C#.
Saludos.
9
Programación / .NET (C#, VB.NET, ASP) / Re: Error en este código: no se puede convertir de 'byte[]' a 'char[]'
en: 29 Diciembre 2023, 03:50 am
Lo intenté hacer de esta otra forma.
using System ;
using System.Text ;
namespace Almacenar_datos_en_un_array_tipo_byte_Consola_01
{
internal class Program
{
static void Main( string [ ] args)
{
// Tamaño máximo del array de bytes.
byte [ ] byteArray
= new byte [ 1024 ] ;
// Convertir de byte[] a char[]
char [ ] charArray = Encoding. Default . GetString ( byteArray) . ToCharArray ( ) ;
Console. WriteLine ( "Escribe un texto y pulsa Enter:" ) ;
// Lee los bytes desde la consola y los guarda en el array.
int leerBytes = Console. In . Read ( charArray, 0 , charArray. Length ) ;
Console. WriteLine ( "Datos almacenados en el array:" ) ;
for ( int i = 0 ; i < leerBytes; i++ )
{
Console. Write ( byteArray[ i] + " " ) ;
}
Console. WriteLine ( ) ;
Console. WriteLine ( "Texto almacenado en el array:" ) ;
// Convierte los bytes a una cadena de texto.
string texto = Encoding. Default . GetString ( byteArray, 0 , leerBytes) ;
Console. WriteLine ( texto) ;
// Pulse cualquier tecla para salir.
Console. ReadKey ( ) ;
}
}
}
Me lee todo como ceros.