Zum Inhalt

Monat: August 2017

string to Stream and vice versa

As I needed this some times in the past few days I think maybe someone else could also benefit from this.

1
2
3
4
5
6
7
8
9
public static Stream GenerateStreamFromString(string s)
{
    MemoryStream stream = new MemoryStream();
    StreamWriter writer = new StreamWriter(stream);
    writer.Write(s);
    writer.Flush();
    stream.Position = 0;
    return stream;
}
1
2
3
4
5
6
7
public static string GenerateStringFromStream(Stream s)
{
    s.Position = 0;
    StreamReader reader = new StreamReader(s);
    string text = reader.ReadToEnd();
    return text;
}

Copying files with exclusions

If you need to copy files from one directory to another one and would like to exclude some files it can be done using PowerShell pretty easy.

1
2
3
4
$source = 'C:\Temp1\*'
$dest = 'C:\Temp2\'
$exclude = @('*.pdb','*.config')
Copy-Item $source $dest -Recurse -Force -Exclude $exclude

In line 3 you can simply add only one specific filename or as in this case simply exclude files based on their extensions.