Outils pour utilisateurs

Outils du site


lang:csharp:readerwriter

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Prochaine révision
Révision précédente
lang:csharp:readerwriter [2016/08/23 18:56] – Création rootlang: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 ''stream.BaseLine''. Ce n'est pas aussi pratique que le ''BinaryReader' mais c'est déjà ça.+Il faut utiliser ''stream.BaseLine''. Ce n'est pas aussi pratique que le ''BinaryReader'' mais c'est déjà ça. 
 + 
 +=====BinaryReader / BinaryWriter===== 
 +<code csharp> 
 +using (BinaryWriter bw = new BinaryWriter(File.Open(fichier, FileMode.Open))) 
 +</code> 
 + 
 +Pour info, il est possible de faire un ''Seek'' au delà de la fin du fichier et d'écrire. La taille du fichier est automatiquement réajustée. 
 + 
 +''BaseStream.Length'' est automatiquement mis à jour sans avoir besoin d'appeler la méthode ''Flush''
 + 
 +=====Lecture de 5 octets sous forme d'un string===== 
 +<code csharp> 
 +byte[] lecture = new byte[5]; 
 +if (iStream.Read(lecture, 0, 5) != 5) 
 +
 +    throw new Exception(); 
 +
 +string result = System.Text.Encoding.ASCII.GetString(lecture); 
 +</code> 
 + 
 +Il n'y a pas besoin du caractère ''null'' en fin de tableau. 
 + 
 +<WRAP center round info 60%> 
 +Par contre, cette méthode crée un ''string'' qui contient des caractères ''null''. Pour s'en débarrasser, il peut être utile de faire un 
 +<code csharp> 
 +if (result.IndexOf('\0') != -1) 
 +
 +  result = result.Substring(0, result.IndexOf('\0')); 
 +
 +</code> 
 +</WRAP> 
 + 
 +=====Remplacement dans un fichier via regex/pattern===== 
 +Sans réfléchir : 
 +<code csharp> 
 +File.WriteAllText("Fichier", Regex.Replace(File.ReadAllText("Fichier"), "Pattern", "Replacement")); 
 +</code> 
 + 
 +Méthode adaptable pour plusieurs remplacements : 
 +<code csharp> 
 +private static void ReplaceTextInFile(string originalFile, string outputFile, string searchTerm, string replaceTerm) 
 +
 +    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, searchTerm,replaceTerm)); 
 +                } 
 +            } 
 +        } 
 +    } 
 +    finally 
 +    { 
 +        if (inputStream != null) 
 +            inputStream.Dispose(); 
 +    } 
 +
 +</code> 
 + 
 +[[https://stackoverflow.com/questions/1915632/open-a-file-and-replace-strings-in-c-sharp|Open a file and replace strings in C# - Stack Overflow]] {{ :lang:csharp:readerwriter:open_a_file_and_replace_strings_in_c_-_stack_overflow_2020-04-27_7_57_16_am_.html |Archive du 16/12/2009 le 27/04/2020}} 
 + 
 +[[https://rules.sonarsource.com/csharp/RSPEC-3966|Objects should not be disposed more than once]] {{ :lang:csharp:readerwriter:c_objects_should_not_be_disposed_more_than_once_2020-04-27_7_58_20_am_.html |Archive le 27/04/2020}} 
 +=====Ajout d'un texte à l'intérieur d'un fichier===== 
 +Pas de méthode miracle malheureusement. 
 +<code csharp> 
 +var sb = new StringBuilder(); 
 +using (var sr = new StreamReader("inputFileName")) 
 +
 +  string line; 
 +  do 
 +  { 
 +    line = sr.ReadLine(); 
 +    sb.AppendLine(line); 
 +  } while (!line.Contains("<Sim Properties>")); 
 + 
 +  sb.Append(myText); 
 +  sb.Append(sr.ReadToEnd()); 
 +
 + 
 +using (var sr = new StreamWriter("outputFileName")) 
 +
 +  sr.Write(sb.ToString()); 
 +
 +</code> 
 + 
 +[[https://stackoverflow.com/questions/14290642/how-to-insert-lines-into-the-middle-of-a-text-file|c# - How to insert lines into the middle of a text file_ - Stack Overflow]] {{ :lang:csharp:readerwriter:c_-_how_to_insert_lines_into_the_middle_of_a_text_file_-_stack_overflow_2020-04-27_7_57_24_am_.html |Archive du 12/01/2013 le 27/04/2020}} 
 + 
 +=====Chargement d'un fichier texte en mémoire===== 
 +<code csharp> 
 +  File.ReadAllLines(nomFichier, Encoding.GetEncoding(1252)); 
 +</code> 
 + 
 +=====Création d'un fichier d'une taille précise===== 
 +<code csharp> 
 +using (var fs = new FileStream("fichier", FileMode.Create, FileAccess.Write, FileShare.None)) 
 +
 +  fs.SetLength(15021); 
 +
 +</code> 
 + 
 +[[https://stackoverflow.com/questions/8416413/create-new-file-with-specific-size|c# - Create new file with specific size - Stack Overflow]] {{ :lang:csharp:readerwriter:c_-_create_new_file_with_specific_size_-_stack_overflow_2020-04-27_7_57_33_am_.html |Archive du 07/12/2011 le 27/04/2020}}
lang/csharp/readerwriter.1471971382.txt.gz · Dernière modification : 2016/08/23 18:56 de root