Freigeben über


Code Snippets for common operations

1. read all the contents of a File

string output = File.ReadAllText(<FilePath>);

2. Write contents to a File

File.WriteAllText(<FilePath>,Encoding);

3. Convert String to Byte Array

byte[] buf = System.Text.Encoding.<Encoding>.GetBytes(string);

4. Convert Byte to String

string output = System.Text.Encoding.<Encoding>.GetString(byte[]);

Comments

  1. Copying one stream to another FileStream _fStream = new FileStream(@"<FilePath>",FileMode.Open); MemoryStream _stream = new MemoryStream(); byte[] buffer = new byte[1024]; int nrBytesWritten = _fStream.Read(buffer, 0, 1024); while (nrBytesWritten > 0) { _stream.Write(buffer, 0, nrBytesWritten);                    nrBytesWritten = _fStream.Read(buffer, 0, 1024);                }