Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

C# Winform store and retrieve array of bytes to Properties,Settings.Default....

Posted on 2016-11-10
7
98 Views
Last Modified: 2016-11-10
I don't see an entry of type array of bytes in the list of settings  in properties for my Visual Studio 2013 project.  I know that there has to be a way of storing an array there, right?  I googled around and don't see an answer.
Any help would be appreciated.
thanks!
0
Comment
Question by:roujesky
  • 3
  • 3
7 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41882483
This is one way to set a byte array.

byte[] myArray;
myArray = new byte[10];
0
 
LVL 33

Expert Comment

by:it_saige
ID: 41882570
One way to do this requires a manual editing of the Settings file after you create your setting; e.g. -

1.  Create your setting as a string (or some other type).Capture.JPG2.  Open the settings file using an XML editor (or notepad).
3.  Change the Type parameter to the type you wish (e.g. - System.Byte[]).Capture.JPG4.  Reload the project settings.Capture.JPGDoing the above allows for me to save a byte array via code without much fuss.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EE_Q28982313.Properties;

namespace EE_Q28982313
{
	class Program
	{
		static void Main(string[] args)
		{
			if (Settings.Default.MyByteArray == default(byte[]))
			{
				Settings.Default.MyByteArray = Encoding.Default.GetBytes("C# is fun!!!");
				Settings.Default.Save();
			}
			Console.WriteLine(Encoding.Default.GetString(Settings.Default.MyByteArray));
			Console.ReadLine();
		}
	}
}

Open in new window

Produces the obvious output.  The user configuration file contains a serialized byte[] (or a base64 binary string):Capture.JPG
-saige-
0
 

Author Comment

by:roujesky
ID: 41882756
I did a lousy job of describing what I want to do.  I have a project in VS2013.  I want to store an array of ints into Properties.Settings.Default...
Looking at the project properties
project.png
Looking at "Type" dropdown, I dont see a way of just storing an array of ints.

thanks!
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 33

Expert Comment

by:it_saige
ID: 41882779
Using my directions, you can do the same thing with an integer array.
1.  Create your setting as a string (or some other type).Capture.JPG2.  Open the settings file using an XML editor (or notepad).
3.  Change the Type parameter to the type you wish (e.g. - System.Int32[]).Capture.JPG4.  Reload the project settings.Capture.JPGAgain, I can save the array via code without much fuss.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EE_Q28982313.Properties;

namespace EE_Q28982313
{
	class Program
	{
		static void Main(string[] args)
		{
			if (Settings.Default.MyByteArray == default(byte[]))
			{
				Settings.Default.MyByteArray = Encoding.Default.GetBytes("C# is fun!!!");
				Settings.Default.Save();
			}

			if (Settings.Default.MyIntegerArray == default(Int32[]))
			{
				Settings.Default.MyIntegerArray = new Int32[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
				Settings.Default.Save();
			}
			Console.WriteLine(Encoding.Default.GetString(Settings.Default.MyByteArray));
			Console.WriteLine("{{ {0} }}", String.Join(", ", Settings.Default.MyIntegerArray));
			Console.ReadLine();
		}
	}
}

Open in new window

Produces the obvious output.Capture.JPGAnd now the user configuration file looks like:Capture.JPG
-saige-
0
 

Author Comment

by:roujesky
ID: 41882843
Thanks it_saige!
I did as you explained.  I made a System.Byte[] in properties called DevMode.
But I get an error when I try to read it out into an array:
byte[] data = Properties.Settings.Default.DevMode;
"Cannot implicitly convert type 'string' to 'byte[]"  
DevMode was originally a string until I modified it like you explained.  I dont know if that means anything....

thanks!
0
 
LVL 33

Accepted Solution

by:
it_saige earned 500 total points
ID: 41882852
Then somehow the program believes that the setting is still a string.  If you use the GetType() method, it should return System.Byte[] and System.Int32[] respectively; e.g. -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EE_Q28982313.Properties;

namespace EE_Q28982313
{
	class Program
	{
		static void Main(string[] args)
		{
			if (Settings.Default.MyByteArray == default(byte[]))
			{
				Settings.Default.MyByteArray = Encoding.Default.GetBytes("C# is fun!!!");
				Settings.Default.Save();
			}

			if (Settings.Default.MyIntegerArray == default(Int32[]))
			{
				Settings.Default.MyIntegerArray = new Int32[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
				Settings.Default.Save();
			}
			Console.WriteLine("Type: {0}; Value: {1}", Settings.Default.MyByteArray.GetType(), Encoding.Default.GetString(Settings.Default.MyByteArray));
			Console.WriteLine("Type: {0}; Value: {{ {1} }}", Settings.Default.MyIntegerArray.GetType(), String.Join(", ", Settings.Default.MyIntegerArray));

			Console.ReadLine();
		}
	}
}

Open in new window

Produces the following output -Capture.JPGThe fact that I can use the byte array setting as a parameter of Encoding.GetString() also shows that it is a byte array.

-saige-
0
 

Author Closing Comment

by:roujesky
ID: 41882906
Thanks!  Got it working.  Not sure why, but in VS i set it to a byte and then in Notepad edited to a System.Byte[], saved and it works.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question