Solved

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

Posted on 2016-11-10
7
30 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
Comment Utility
This is one way to set a byte array.

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

Expert Comment

by:it_saige
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 32

Expert Comment

by:it_saige
Comment Utility
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
Comment Utility
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 32

Accepted Solution

by:
it_saige earned 500 total points
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 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