2

My String variable only stores 4096 characters, I need to store more, how can i achieve that?

Below is what i am trying to do

ServiceController[] myServices = ServiceController.GetServices();
String ServiceList = "";

foreach (ServiceController service in myServices)
{

      ServiceList += service.DisplayName + "|||";

} 
return ServiceList;

When the variable is returned, it only stores 4096 characters and rest are trimmed off.

P.S. I need them in one variable as I am making a URL out of them and passing to my webservice.

Uzair Shahid
  • 49
  • 1
  • 3
  • .Net strings have a maximum length of 2^31 code points, not 2^12. – Michael Petrotta Jun 30 '12 at 05:02
  • You could use a StringBuilder rather than a simple string, but I strongle recommend you to read the answers and think on a new solution. URLs should be as short as possible. – Andre Calil Jun 30 '12 at 05:05
  • So why is it storing just 2^12 characters? – Uzair Shahid Jun 30 '12 at 05:09
  • It's not, @Uzair. Strings can store much, *much* more than that. Are you sure that's just not the natural length of the data you're storing in that string? What is the value of `ServiceList.Length` at the end of the method above? – Michael Petrotta Jun 30 '12 at 05:38
  • 3
    At what point in the code did you measure the length of the string? – Gabe Jun 30 '12 at 05:45

4 Answers4

4

I need them in one variable as I am making a URL out of them and passing to my webservice.

No, don't do that!

A 4096 character URL is a very bad idea and is not guaranteed to work.

Extremely long URLs are usually a mistake. URLs over 2,000 characters will not work in the most popular web browser. Don't use them if you intend your site to work for the majority of Internet users.

(source)

Make a shortened URL that contains an id. Store the rest of the information in a database with the short id as the key.

Related

Community
  • 1
  • 1
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • actually i am just calling a webpage from within my code and passing this variable as a parameter. That webpage only explodes the string and inserts into database. I have no other option as the windows service dont have access to the database. I need to go this path... – Uzair Shahid Jun 30 '12 at 05:09
  • @UzairShahid: .NET strings do not have a maximum length of 4096 characters. You are looking for the error in the wrong place. – Mark Byers Jun 30 '12 at 05:12
3

.NET string length limit is 2 billion characters.

Browsers do have a limit on how long of a URL they will accept, and the length limit is different across browser implementations. IE's limit is typically the shortest, at around 2k last time I checked in the IE6 era. Firefox and Chrome are considerably higher than that, but there is still a limit.

dthorpe
  • 35,318
  • 5
  • 75
  • 119
  • Thanks for that, but why my variable only stores 4096 characters?? – Uzair Shahid Jun 30 '12 at 05:12
  • 1
    How are you observing that the string only contains 4096 characters? are you inspecting the variable in a debugger before the function returns? Or are you looking at some web result after your value has been through dozens of system layers? – dthorpe Jul 02 '12 at 16:21
0

Strings in C# has about 2Gig limit.so there is no problem with your string variable

Abadis
  • 2,671
  • 5
  • 28
  • 42
0

Your problem is elsewhere - but it is not possible to reliably use a URL that uses more than 2000 characters, you will need another approach entirely - see this SO answer: What is the maximum length of a URL?

(Also for building large strings use a StringBuilder instead)

Community
  • 1
  • 1
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335