How to read a embedded resource to String in C# and VB.NET

To read a embedded resource to String in C# and VB.NET you can use the snippet below.
Keep in mind that this snippet is using Encoding.Default which might not always work in all situations correctly.

Sample C#

Sample VB.NET (autogenerated)

Please rate this snippet

Top 10 reads of April 2015

These are the Top 10 reads of April 2015.

  • How to search stored procedures using MS-SQL
  • How to manually populate a multicolumn devexpress TreeList using C# and VB.NET
  • How to download a file using Python
  • How to drop Primary Key of a Table using MSSQL
  • How to read a String to Textreader using C# and VB.NET
  • How to add or remove a class on hover using jQuery
  • How to use the HTML5 datalist tag
  • How to reload a ViewController in IOS using Objective-C
  • How to get current Domain of the PC in C# and VB.NET
  • What is the difference between typeof(), GetType() and IS in C# and VB.NET
  • thanks to all visitors, bookmark us, share us, talk about us, Follow us!, Like us! let’s grow bigger!

    Please rate this snippet

    Top 10 reads of March 2015

    These are the Top 10 reads of March 2015.

  • How to add or remove a class on hover using jQuery
  • How to use the HTML5 datalist tag
  • How to reload a ViewController in IOS using Objective-C
  • How to get current Domain of the PC in C# and VB.NET
  • What is the difference between typeof(), GetType() and IS in C# and VB.NET
  • How to split a delimited string to multiple rows using MSSQL
  • How to deep copy a IList in C# and VB.NET
  • How to make a multicolumn listbox in C# and VB.NET
  • How to open a Website in the default Browser in C# and VB.NET
  • How to delete .svn files and folders using Windows Batch Script
  • thanks to all visitors, bookmark us, share us, talk about us, Follow us!, Like us! let’s grow bigger!

    Please rate this snippet

    How to read a File to Textreader in C# and VB.NET

    To read a File to Textreader in C# and VB.NET you can use the following snippet.

    Sample C#

    TextReader reader = File.OpenText(@"C:\dummy.txt");
    

    Sample VB.NET

    Dim reader As TextReader = File.OpenText(@"C:\dummy.txt")
    

    Please rate this snippet

    How to read a file to string in Java

    To read a file to string in Java you can use the following snippet.

    Sample Java

    public static string readFileToString(FileInputStream _fileInputStream)
    {
    	try {  
    		DataInputStream dataInputStream = new DataInputStream (_fileInputStream);  
    		byte[] Bytes = new byte[dataInputStream.available ()];  
    		dataInputStream.readFully (Bytes);  
    		dataInputStream.close ();  
    		String result = new String (Bytes, 0, Bytes.length, "Cp850");  
    		return result;  
    	}  
    	catch (Exception e) {  
    		e.printStackTrace();  
    		return "";
    	} 
    }
    

    Please rate this snippet

    How to read a embedded resource image in C# and VB.NET

    To read a embedded resource image in C# and VB.NET you can use the following snippet.

    Sample C#

    public static Image ReadEmbeddedRessourceImage(Assembly assembly, string searchPattern)
    {
    	var resourceName = assembly.GetManifestResourceNames().FirstOrDefault(x => x.Contains(searchPattern));
    	using (var stream = assembly.GetManifestResourceStream(resourceName))
    	{
    		if (stream != null) return Image.FromStream(stream);
    	}
    	return null;
    }
    

    Sample VB.NET

    Public Shared Function ReadEmbeddedRessourceImage(assembly As Assembly, searchPattern As String) As Image
    	Dim resourceName = assembly.GetManifestResourceNames().FirstOrDefault(Function(x) x.Contains(searchPattern))
    	Using stream = assembly.GetManifestResourceStream(resourceName)
    		If stream IsNot Nothing Then
    			Return Image.FromStream(stream)
    		End If
    	End Using
    	Return Nothing
    End Function
    

    Please rate this snippet

    How to read a file into a bytearray in Java

    To read a file into a bytearray in Java you can use the following snippet.

    Sample Java

    public static byte[] ReadFileIntoByteArray(File fileName) throws IOException {
        InputStream inputStream = new FileInputStream(fileName);
        byte[] bytes;
            
        try {
            long fileLength = fileName.length();
            if (fileLength > Integer.MAX_VALUE) {
                bytes = new byte[(int)fileLength];
    			int readOffset = 0;
    			int readCount = 0;
    			while ((readCount=inputStream.read(bytes, readOffset, bytes.length-readOffset)) >= 0 && readOffset < bytes.length ) {
    				readOffset += readCount;
    			}
    			if (bytes.length > readOffset) {
    				throw new IOException("Could not completely read file " + fileName.getName());
    			}
            }
    		else{
    			throw new IOException("File " + fileName.getName() + " too large (>2GB)!");
    		}
        }
        finally {
            inputStream.close();
        }
        return bytes;
    }
    

    Please rate this snippet

    How to read a file into a bytearray in C# and VB.NET

    To read a file into a bytearray in C# and VB.NET you can use the following snippet.

    Sample C#

    var bytes = System.IO.File.ReadAllBytes(fileName);
    

    Sample VB.NET

    Dim bytes = System.IO.File.ReadAllBytes(fileName)
    

    Please rate this snippet

    How to select top 10 reads per month in WordPress

    To select top 10 reads per month in WordPress, you need to use the Plugin CountPerDay.
    The following mysql snippet will do the rest.

    Sample MySQL

    SELECT COUNT(page), wp_posts.post_title, wp_posts.post_name from wp_cpd_counter join wp_posts ON wp_posts.id = wp_cpd_counter.page WHERE wp_posts.post_date between '2014-06-01' AND '2014-06-30' GROUP BY page ORDER BY 1 desc LIMIT 10
    

    Please rate this snippet