Solved

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

Posted on 2016-11-10
7
143 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
[X]
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
  • 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 34

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

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 34

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Suggested Courses

734 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