none
Programa para conexão WiFi RRS feed

  • Pergunta

  • Galera to num trampo novo de estagiario, e programando em C# linguagem que eu nunca tinha nem mexido na minha vida ... e me mandaram esse programa pra dar um jeito ...... so que naum manjo nd de C# .... ja to ha 2 semanas me matando e continuo sem sucesso em algum avanço.... galera realmente to precisando de ajuda ......... esse programa tem que puxar dados de uma camera por FTP  ..... mas está dando erro na função baixar ...... e a partir dai vai virando uma bola de neve....... se alguem puder me ajudar realmente agradeço .

     

     

     

    using System;

    using System.Collections.Generic;

    using System.IO;

    using System.Net;

    using System.Net.Sockets;

    using System.Net.FtpClient;

    using System.Text;

    using System.Net.NetworkInformation;

    using wcl;

     

    namespace wireless

    {

        class Program

        {

            static void Main(string[] args)

            {

                bool exit = false;  //variavel de controle de sessao 

                wclAPI api = new wclAPI();  //API de acesso as funcoes WiFi 

                wclWiFiManager manager = new wclWiFiManager();  //manipulador das funções WiFi 

                wclWiFiInterfaces ifaces = new wclWiFiInterfaces();//lista de interfaces WiFi

                wclWiFiInterface iface = new wclWiFiInterface();    //interface de conexao WiFi

                wclWiFiNetworks Networks = new wclWiFiNetworks();   //lista de redes WiFi

     

                api.Load(); //carrega o API

                manager.Open(); //carrega o manipulador

     

                uint Reason = 0;    // variavel de referencia

                int Res = manager.EnumInterfaces(ifaces, ref Reason);   //vaiavel de enumeracao

     

                while (!exit)   //laco de repeticao para manter programa aberto

                {

                    //Console.Clear();    //limpa o console

     

                    Console.WriteLine("WiFi Manager - Midcom"); //escreve mensagem na tela

                    Console.WriteLine("");  //escreve mensagem na tela

     

                    for (uint ifacesCount = 0; ifacesCount < ifaces.Count; ifacesCount++) //laco de repeticao para acessar cada interface

                    {

                        iface.Guid = new Guid(ifaces[ifacesCount].Guid.ToString());   //le informacoes hexadecimais da interface

                        Console.WriteLine("Acessando " + ifaces[ifacesCount].Description.ToString() + "...");   //escreve pasta sendo acessada

     

                        List<String> Profiles = new List<String>(); //lista que vai receber os profiles cadastrados

     

                        Res = iface.EnumProfiles(Profiles, ref Reason); //enumera os profiles

     

                        for (uint profilesCount = 0; profilesCount < Profiles.Count; profilesCount++)   //laco de repeticao para acessar cada profile

                        {

                            Res = iface.EnumAvailableNetworks(Networks, wclWiFiNetworkFlag.nfNone, ref Reason); //enumera as redes

     

                            Console.WriteLine("Procurando pela rede " + Profiles[(int)profilesCount] + "..."); //mostra profile sendo buscado

     

                            for (uint networksCount = 0; networksCount < Networks.Count; networksCount++)   //laco de repeticao para acessar cada rede

                            {

                                if (Networks[networksCount].Profile == Profiles[(int)profilesCount])    //verifica se rede existe nos profiles

                                {

                                    Console.WriteLine("Tentando conectar-se a " + Profiles[(int)profilesCount] + "...\n");    //mostra profile sendo conectado

     

                                    iface.Connect(wclWiFiConnectionMode.cmProfile,

                                    wclWiFiBssType.btAny, Profiles[(int)profilesCount], ref Reason); //tenta conectar no profile

     

                                    Console.WriteLine("Conectado a " + Profiles[(int)profilesCount] + "!"); //mostra status da conexao

                                    Console.WriteLine(iface);

                                    Console.ReadLine();

     

                                    try

                                    {

                                        baixar(); //invoca o metodo de download

                                    }

                                    catch (Exception e)

                                    {

                                        Console.WriteLine(e); //trata um possivvel erro

                                    }

                                    Console.WriteLine("");  //escreve mensagem na tela

     

                                    Console.ReadLine(); //aguarda entrada do usuario para prosseguir

                                }

                            }

                        }

                    }

                }

     

                Console.WriteLine("");  //escreve mensagem na tela

                Console.Write("Continuar...");  //escreve mensagem na tela

     

                if (Console.ReadLine() == "")   //condicional de saida

                {

                    exit = true;    //altera estado  da variavel de controle para sair

                }

     

                manager.Close();    //finaliza o manipulador

                api.Unload();   //finaliza o API

            }

     

            static bool baixar()

            {

                //foreach (WlanClient.WlanInterface wlanInterface in wlan.Interfaces)

                //{

                //    Wlan.Dot11Ssid ssid = wlanInterface.CurrentConnection.wlanAssociationAttributes.dot11Ssid;

                //    connectedSsids.Add(new String(Encoding.ASCII.GetChars(ssid.SSID, 0, (int)ssid.SSIDLength)));

                //}

              

                try

                {

                    string[] files = GetFileList();

                    Download(null);

                   //dando erro aki

                    foreach(string file in files)

                    {

                        Download(file);

                    }

     

                    return true;

                }

                catch (Exception e)

                {

                    Console.WriteLine(e);

                    return false;

                }

            }

     

            //software

     

     

            static string[] GetFileList()

            {

                string[] downloadFiles;

                StringBuilder result = new StringBuilder();

                WebResponse response = null;

                StreamReader reader = null;

                try

                {

     

     

     

                   

                    FtpWebRequest reqFTP;

                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://192.168.8.22/mnt/auto/CF/ncmf"));

                    Console.WriteLine(reqFTP);

                    reqFTP.UseBinary = true;

                    reqFTP.Credentials = new NetworkCredential("root","root");

                    reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;

                    reqFTP.Proxy = null;

                    reqFTP.KeepAlive = false;

                    reqFTP.UsePassive = false;

     

                    response = reqFTP.GetResponse();

                    reader = new StreamReader(response.GetResponseStream());

                    string line = reader.ReadLine();

     

                    while (line != null)

                    {

     

                        result.Append(line);

                        result.Append("\n");

                        line = reader.ReadLine();

                    }

                    // to remove the trailing '\n'

                    result.Remove(result.ToString().LastIndexOf('\n'), 1);

                    return result.ToString().Split('\n');

                }

                catch (Exception ex)

                {

                    if (reader != null)

                    {

                        reader.Close();

                    }

                    else

                    {

                    }

     

                    if (response != null)

                    {

                        response.Close();

                    }

                    else

                    {

                    }

     

                    downloadFiles = null;

                    return downloadFiles;

                }

            }

     

            static void Download(string file)

            {

                try

                {

                    string uri = "ftp://192.168.8.22/mnt/auto/CF/ncmf" + file;

                    Uri serverUri = new Uri(uri);

     

                    if (serverUri.Scheme != Uri.UriSchemeFtp)

                    {

                        return;

                    }

                    FtpWebRequest reqFTP;

     

                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://192.168.8.22/mnt/auto/CF/ncmf" + file));

                    reqFTP.Credentials = new NetworkCredential("root", "root");

                    reqFTP.KeepAlive = false;

                    reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;

                    reqFTP.UseBinary = true;

                    reqFTP.Proxy = null;

                    reqFTP.UsePassive = false;

                    FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

                    Stream responseStream = response.GetResponseStream();;

                    FileStream writeStream = new

                    FileStream("C:\\" + "" + file, FileMode.Create);

                    int Length = 2048;

                    Byte[] buffer = new

                    Byte[Length];

                    int bytesRead = responseStream.Read(buffer, 0, Length);

                    while (bytesRead > 0)

                    {

                        writeStream.Write(buffer, 0, bytesRead);

                        bytesRead = responseStream.Read(buffer, 0, Length);

                    }

                    writeStream.Close();

                    response.Close();

                }

                catch (WebException wEx)

                {

                    Console.WriteLine(wEx.Message, "Download Error");

                }

                catch (Exception ex)

                {

                    Console.WriteLine(ex.Message, "Download Error");

                }

            }

    terça-feira, 30 de agosto de 2011 01:21

Todas as Respostas