public static ReadStreamToString ( System input, String &str, |
||
input | System | |
str | String | |
Encode | ||
Résultat | void |
static public String ReadFileToString(String FileName, Encoding encode) { if (!System.IO.File.Exists(FileName)) { string name = System.IO.Path.GetFileName(FileName); string result = null; var assembly = Assembly.GetExecutingAssembly(); var assemblyName = assembly.GetName().Name; //string[] names = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames(); using (var textStream = assembly.GetManifestResourceStream(assemblyName + ".Resources." + name)) { //var textStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name); if (textStream != null) { using (var reader = new StreamReader(textStream, Encoding.UTF8)) { result = reader.ReadToEnd(); } } } return(result); } else { FileStream fs = null; String str; int times = 0; while (true) { try { fs = new FileStream(FileName, FileMode.Open, FileAccess.Read); Stream.ReadStreamToString(fs, out str, encode); fs.Close(); return(str); } catch (IOException e) { uint hResult = (uint)System.Runtime.InteropServices.Marshal.GetHRForException(e); if (hResult == ERR_PROCESS_CANNOT_ACCESS_FILE) { if (times > 10) { //Maybe another program has some trouble with file //We must exit now throw e; } System.Threading.Thread.Sleep(200); times++; } else { throw e; } } } } }
static public String ReadFileToString(String FileName, Encoding encode) { FileStream fs = null; String str; int times = 0; while (true) { try { fs = new FileStream(FileName, FileMode.Open, FileAccess.Read); Stream.ReadStreamToString(fs, out str, encode); fs.Close(); return(str); } catch (IOException e) { uint hResult = (uint)System.Runtime.InteropServices.Marshal.GetHRForException(e); if (hResult == ERR_PROCESS_CANNOT_ACCESS_FILE) { if (times > 10) { //Maybe another program has some trouble with file //We must exit now throw e; } System.Threading.Thread.Sleep(200); times++; } else { throw e; } } } }