Overblog Suivre ce blog
Editer l'article Administration Créer mon blog

Présentation

  • : Romagny13 - Du .NET,du pur .NET
  • Romagny13 - Du .NET,du pur .NET
  • : Cycle de progression Faire (quelque chose qui marche) -> comprendre ce que l’on fait/comment cela marche -> pousser plus loin les notions
  • Contact

Recherche

Articles Récents

13 avril 2014 7 13 /04 /avril /2014 18:18

Stream

  • Stream

    • FileStream

    • IsolatedStorageFileStream

    • NetWorkStream

    • SerialPort

     

    Services de flot de données

     

    • AuthenticatedStream

    • CryptoStream

    • BufferedStream

    • MemoryStream  et UnmananagedMemoryStream

    • DeflateStream et GZipStream

       

Typer le flot de données

  • BinaryReader/BinaryWriter

  • TextReader/TextWriter

    • StringReader/StringWriter

    • StreamReader/StreamWriter

       

 

Info

  • FileSystemInfo

    • DirectoryInfo

    • FileInfo

+ Exceptions,Types(Enum)

File

Simple lecture / Ecriture dans des fichiers

string value =  File.ReadAllText(path);

byte[] data = File.ReadAllBytes(path);

foreach (string line in File.ReadAllLines(path)) { }

FileStream fileStream = File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);

StreamReader streamReader = File.OpenText(path);

// Exceptions

try { return File.ReadAllText(path); }

catch (DirectoryNotFoundException) { }

catch (FileNotFoundException) { }

 

File.WriteAllText(path, value);

File.WriteAllBytes(path,data);

StreamWriter streamWriter = File.AppendText(path);

 

FileStream

Ce qu’il faut pour lire/écrire un flot de données :

Stream

Encodage (UNICODE) + normes UTF-8, etc. (exemple System.Text.UTF8Encoding)

Tableau de byte [] obtenu selon l’encodage

Méthodes lecture/écriture du Stream

        public void WriteFile(string value,string path)

        {

            using (FileStream stream = File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))

            {

                byte[] data = new UTF8Encoding(true).GetBytes(value);

                stream.Write(data, 0, data.Length);

            }

        }

        public string ReadFile(string path)

        {

            string result = string.Empty;

            using (FileStream stream = File.OpenRead(path))

            {

                byte[] data = new byte[stream.Length];

                for (int index = 0; index < stream.Length; index++)

                    data[index] = (byte)stream.ReadByte();

                

                result = Encoding.UTF8.GetString(data); 

            }

            return result;

        }

 

Asynchrone

        public  async Task WriteFileAsync(string path,string value)

        {     

                   using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write,FileShare.None, 4096, true))

            {

                byte[] data = new UTF8Encoding(true).GetBytes(value);

                stream.Write(data, 0, data.Length);

 

                await stream.WriteAsync(data, 0, data.Length);

            }

        }

        public async Task<string> ReadFileAsync(string path)

        {

            using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite))

            {

                byte[] bytes = new byte[stream.Length];

                int count = (int)stream.Length;

 

                await stream.ReadAsync(bytes, 0, (int)stream.Length);

                return Encoding.UTF8.GetString(bytes);

            }

        }

StreamReader/StreamWriter

public void WriteStream(string value,string path)

        {

            using (StreamWriter streamWriter = File.CreateText(path))

            {

                streamWriter.Write(value);

            }

        }

        public async Task WriteStreamAsync(string value, string path)

        {

            using (StreamWriter streamWriter = File.CreateText(path))

            {

               await streamWriter.WriteAsync(value);

            }

        }

        public string ReadStream(string path)

        {

            using (StreamReader streamReader = File.OpenText(path))

            {

                return streamReader.ReadToEnd();

            }

        }

        public async Task<string> ReadStreamAsync(string path)

        {

            using (StreamReader streamReader = File.OpenText(path))

            {

                return await streamReader.ReadToEndAsync();

            }

        }

WebRequest/WebResponse

flot de données distant  

  public string GetWebResponse(string uri)

        {

            WebRequest request = WebRequest.Create(uri);

            using (WebResponse response = request.GetResponse())

            {

                using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))

                {

                    return streamReader.ReadToEnd();

                }

            }

        }

        public async Task<string> GetWebResponseAsync(string uri)

        {

            WebRequest request = WebRequest.Create(uri);

            using (WebResponse response = await request.GetResponseAsync())

            {

                using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))

                {

                    return streamReader.ReadToEnd();

                }

            }

        }

 

FtpWebRequest

public async Task UploadFileAsync(string uri,string login,string password,string value)

        {

            FtpWebRequest request = WebRequest.Create(uri) as FtpWebRequest;

            request.Credentials = new NetworkCredential(login, password);

            request.Method = WebRequestMethods.Ftp.UploadFile;

 

            using (Stream stream = await request.GetRequestStreamAsync())

            {

                byte[] data = new UTF8Encoding(true).GetBytes(value);

                await stream.WriteAsync(data, 0, data.Length);

            }           

        }

 

Utilisation .Exemple

string value = @"<!DOCTYPE html>

<html xmlns=""http://www.w3.org/1999/xhtml"">

    <head>

        <meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"" />

    </head>

    <body>

        <h1>Page de test</h1>

    </body>

</html>";

 

 

 

await UploadFileAsync(

"ftp://ftp.nomserveur.org/index.html", "votreidentifiant", "votrepassword",value);  

 

 

+ Path

 

http://msdn.microsoft.com/fr-fr/library/system.io.path(v=vs.110).aspx

 

 

Partager cet article

Repost 0
Published by Romagny13 - dans C
commenter cet article

commentaires