Menú

Mostrar Mensajes

Esta sección te permite ver todos los mensajes escritos por este usuario. Ten en cuenta que sólo puedes ver los mensajes escritos en zonas a las que tienes acceso en este momento.

Mostrar Mensajes Menú

Mensajes - d1dac

#1
.NET (C#, VB.NET, ASP) / Aporte-Duda DownNET
6 Noviembre 2012, 03:10 AM
Hola comunidad, hace ya un tiempo quise codear un programa que apagara todas las maquinas de la red local, lo había abandonado pero lo volví a retomar y parece que va en buen camino.

La primera versión que hice tenia un defecto que al enviar la señal de apagado a las maquinas, apagaba por ejemplo maquina 1,maquina 2,m 3,m 4,mi maquina. pero ya no apagaba el resto. m 5,m6
ya que mi maquina estaba en el medio.

Decidí arreglarlo y parece estar ok. pero me gustaría saber en que puede ser optimizado o añadir opciones, o incluso si es que funciona

Aqui mi code

Código (csharp) [Seleccionar]

using System;
using System.Text;
using System.Diagnostics;
using System.Collections;
using System.IO;
using System.Threading;

namespace ConsoleApplication1
{
   class Program
   {
       static Process u;
       static Process v;
       static string usuario;
       static string[] maquina;
       static int i=0;

       static void Main(string[] args)
       {
           //*-*-*-*-*-*-*-*-*-*-*
           //Lan Dow Net by D1dac
           //*-*-*-*-*-*-*-*-*-*-*
           try
           {
               File.Delete("user.txt");
               File.Delete("maquinas.txt");
           }
           catch { }

           Thread nv = new Thread(netView);
           nv.Start();

           Thread nu = new Thread(netUser);
           nu.Start();

           Console.ForegroundColor = ConsoleColor.Green;
           Console.Title = "Net Shutdown by D1dac 1.0";


           Console.WriteLine("En cuanto tiempo quieres que se apague" + "\nPD:Tu maquina tambien se incluye en la lista");
           string tiempo=Console.ReadLine();

           
           for (int a = 0; a <= i-1; a++)
           {
               if (!maquina[a].Equals((usuario)))
               {
                   ProcessStartInfo info = new ProcessStartInfo("CMD.EXE", "/c shutdown -s -t " + tiempo + " -m " + maquina[a]);
                   info.WindowStyle = ProcessWindowStyle.Hidden;
                   info.Verb = "open";
                   Process.Start(info);
               }
           }

           ProcessStartInfo autoS = new ProcessStartInfo("CMD.EXE", "/c  shutdown -s -t " + tiempo);
           autoS.WindowStyle = ProcessWindowStyle.Hidden;
           autoS.Verb = "open";
           Process.Start(autoS);
           
       }



       static void netView()
       {
           ProcessStartInfo info1 = new ProcessStartInfo("CMD.EXE", "/C net view > maquinas.txt");
           info1.Verb = "open";
           info1.WindowStyle = ProcessWindowStyle.Hidden;
           v= Process.Start(info1);

           string line;
           maquina = new string[50];

           v.WaitForExit();

           StreamReader file = new StreamReader("maquinas.txt");

           while ((line = file.ReadLine()) != null)
           {
               if (line.Contains("\\"))
               {
                   maquina[i] = (line.Substring(2, line.Length - 2)).TrimEnd();
                   Console.WriteLine(i + " : " + maquina[i]);
                   i++;
               }
           }

           file.Close();

           try { File.Delete("maquinas.txt"); } catch{};
       }

       static void netUser()
       {
           ProcessStartInfo user = new ProcessStartInfo("CMD.EXE", "/C net user > user.txt");
           user.Verb = "open";
           user.WindowStyle = ProcessWindowStyle.Hidden;
           u= Process.Start(user);

           u.WaitForExit();
 
           string path = "user.txt";
           string readText = File.ReadAllText(path, Encoding.UTF8);
           usuario = readText.Substring(26, 10).ToString().ToUpper();

           try { File.Delete("user.txt"); }catch { };
       }

       
   }
}


Cabe resaltar que debes tener privilegios en la maquina remota.