Solved

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

Posted on 2016-11-10
7
76 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Syntax Error 2 62
Hey Audio! 3 27
performance tune IIS 10 on win 2016 that only runs one website 4 58
Call Controller Action Method from ASPX 2 13
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…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

813 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

16 Experts available now in Live!

Get 1:1 Help Now