Solved

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

Posted on 2016-11-10
7
58 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 62

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to iterate over object and get property name and value 4 26
Please explain "Multi-Tenant Services" 5 63
Remove greater than sign 3 44
Export import database 4 43
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now