lang:csharp:readerwriter
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
lang:csharp:readerwriter [2016/08/23 18:56] – Création root | lang:csharp:readerwriter [2020/04/27 08:04] (Version actuelle) – Conversion de <note> vers <WRAP> root | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
=====Lecture de données binaire avec un StreamReader===== | =====Lecture de données binaire avec un StreamReader===== | ||
- | Il faut utiliser '' | + | Il faut utiliser '' |
+ | |||
+ | =====BinaryReader / BinaryWriter===== | ||
+ | <code csharp> | ||
+ | using (BinaryWriter bw = new BinaryWriter(File.Open(fichier, | ||
+ | </ | ||
+ | |||
+ | Pour info, il est possible de faire un '' | ||
+ | |||
+ | '' | ||
+ | |||
+ | =====Lecture de 5 octets sous forme d'un string===== | ||
+ | <code csharp> | ||
+ | byte[] lecture = new byte[5]; | ||
+ | if (iStream.Read(lecture, | ||
+ | { | ||
+ | throw new Exception(); | ||
+ | } | ||
+ | string result = System.Text.Encoding.ASCII.GetString(lecture); | ||
+ | </ | ||
+ | |||
+ | Il n'y a pas besoin du caractère '' | ||
+ | |||
+ | <WRAP center round info 60%> | ||
+ | Par contre, cette méthode crée un '' | ||
+ | <code csharp> | ||
+ | if (result.IndexOf(' | ||
+ | { | ||
+ | result = result.Substring(0, | ||
+ | } | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | =====Remplacement dans un fichier via regex/ | ||
+ | Sans réfléchir : | ||
+ | <code csharp> | ||
+ | File.WriteAllText(" | ||
+ | </ | ||
+ | |||
+ | Méthode adaptable pour plusieurs remplacements : | ||
+ | <code csharp> | ||
+ | private static void ReplaceTextInFile(string originalFile, | ||
+ | { | ||
+ | string tempLineValue; | ||
+ | FileStream inputStream = null; | ||
+ | try | ||
+ | { | ||
+ | inputStream = File.OpenRead(originalFile) | ||
+ | using (StreamReader inputReader = new StreamReader(inputStream)) | ||
+ | { | ||
+ | inputStream = null; | ||
+ | using (StreamWriter outputWriter = File.AppendText(outputFile)) | ||
+ | { | ||
+ | while(null != (tempLineValue = inputReader.ReadLine())) | ||
+ | { | ||
+ | outputWriter.WriteLine(Regex.Replace(tempLineValue, | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | finally | ||
+ | { | ||
+ | if (inputStream != null) | ||
+ | inputStream.Dispose(); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | [[https:// | ||
+ | =====Ajout d'un texte à l' | ||
+ | Pas de méthode miracle malheureusement. | ||
+ | <code csharp> | ||
+ | var sb = new StringBuilder(); | ||
+ | using (var sr = new StreamReader(" | ||
+ | { | ||
+ | string line; | ||
+ | do | ||
+ | { | ||
+ | line = sr.ReadLine(); | ||
+ | sb.AppendLine(line); | ||
+ | } while (!line.Contains("< | ||
+ | |||
+ | sb.Append(myText); | ||
+ | sb.Append(sr.ReadToEnd()); | ||
+ | } | ||
+ | |||
+ | using (var sr = new StreamWriter(" | ||
+ | { | ||
+ | sr.Write(sb.ToString()); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | =====Chargement d'un fichier texte en mémoire===== | ||
+ | <code csharp> | ||
+ | File.ReadAllLines(nomFichier, | ||
+ | </ | ||
+ | |||
+ | =====Création d'un fichier d'une taille précise===== | ||
+ | <code csharp> | ||
+ | using (var fs = new FileStream(" | ||
+ | { | ||
+ | fs.SetLength(15021); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | [[https:// |
lang/csharp/readerwriter.1471971382.txt.gz · Dernière modification : 2016/08/23 18:56 de root