Longest line of C#; isolated storage

P

Peter Webb

If Nicholas Paldino reads this, please don't have an apoplectic fit.

Other professionals with weak stomachs may want to look away.

My problem was that I needed to set up some files that the user can read,
write and delete. There are 20 of these, all xml, about 30k each.

I couldn't work out where I could read them from when the package is first
installed or run. Nor could I understand either of Mr Paldino's suggestions.

So used Plan B. I wrote a small program to basically read in all each xml
files, map double quotes to single quote, slashes to pipes, and end of lines
to tildes, and write them out to a text file, preceded by String
Filename[counter]=" and terminated by ";

This gave me a single valid line of C# code in a text file, which I cut and
paste into my source.

Its 623,060 characters long. FWIW, it starts:

filename[0] = "Default configuration"; filecontents[0] = "<?xml
version='1.0' encoding='utf-8'?>~<saveStructure
xmlns:xsi='http:||www.w3.org|2001|XMLSchema-instance'
xmlns:xsd='http:||www.w3.org|2001|XMLSchema'>~ <alist>~ <textaslot>~
<clickarea>~ <Location>~ <X>404<|X>~

At runtime I just reverse the substitutions and write the 20 strings out to
20 files with the appropriate names. Works a treat, and is virtually
instantaneous to do.

The IDE accepts this just fine. It does warn me that the program contains an
extremely long line (623,060 is extremely long?) and the UI may may be slow,
which it is, but only if the line is on the screen. I stick it at the end
after a bunch of blank lines so its no problem.

Now, a puritan may argue that this is actually 40 C# statements concatenated
together, which is true, but one of the String statements is 53k long, and
unless there is something about carriage returns and strings I don't know,
cannot be any shorter.

So, how many of you so-called "experts" have ever written a line of C# which
is even 53k long, let alone 623,060 characters long? Huh?

Which brings me to my question about isolated storage.

Its working just fine for me, and thankyou to whoever suggested it. It did
seem to suffer from one problem, which is that it is only available to the
runtime. This meant I had to write some basic file management functionality
within my app for debugging purposes - clearing directories and files, for
example.

I don't want to have to do this again for the next project I decide to
undertake.

It seems that most people who use isolated storage to hold a file system
would have similar requirements during development.

Has anybody developed/provided a basic file management UI for isolated
storage that I can cut and paste into my code with a conditional assembly
flag, or is there perhaps something that can run externally that can help?

Thanks


Peter Webb
(author of longest C# statement ever)
 
R

Roger Frost

Oh how I wish I had got in on this thread sooner...

--
Roger Frost
"Logic Is Syntax Independent"



Peter Webb said:
If Nicholas Paldino reads this, please don't have an apoplectic fit.

Other professionals with weak stomachs may want to look away.

My problem was that I needed to set up some files that the user can read,
write and delete. There are 20 of these, all xml, about 30k each.

I couldn't work out where I could read them from when the package is first
installed or run. Nor could I understand either of Mr Paldino's
suggestions.

So used Plan B. I wrote a small program to basically read in all each xml
files, map double quotes to single quote, slashes to pipes, and end of
lines to tildes, and write them out to a text file, preceded by String
Filename[counter]=" and terminated by ";

This gave me a single valid line of C# code in a text file, which I cut
and paste into my source.

Its 623,060 characters long. FWIW, it starts:

filename[0] = "Default configuration"; filecontents[0] = "<?xml
version='1.0' encoding='utf-8'?>~<saveStructure
xmlns:xsi='http:||www.w3.org|2001|XMLSchema-instance'
xmlns:xsd='http:||www.w3.org|2001|XMLSchema'>~ <alist>~ <textaslot>~
<clickarea>~ <Location>~ <X>404<|X>~

At runtime I just reverse the substitutions and write the 20 strings out
to 20 files with the appropriate names. Works a treat, and is virtually
instantaneous to do.

The IDE accepts this just fine. It does warn me that the program contains
an extremely long line (623,060 is extremely long?) and the UI may may be
slow, which it is, but only if the line is on the screen. I stick it at
the end after a bunch of blank lines so its no problem.

Now, a puritan may argue that this is actually 40 C# statements
concatenated together, which is true, but one of the String statements is
53k long, and unless there is something about carriage returns and strings
I don't know, cannot be any shorter.

So, how many of you so-called "experts" have ever written a line of C#
which is even 53k long, let alone 623,060 characters long? Huh?

Which brings me to my question about isolated storage.

Its working just fine for me, and thankyou to whoever suggested it. It did
seem to suffer from one problem, which is that it is only available to the
runtime. This meant I had to write some basic file management
functionality within my app for debugging purposes - clearing directories
and files, for example.

I don't want to have to do this again for the next project I decide to
undertake.

It seems that most people who use isolated storage to hold a file system
would have similar requirements during development.

Has anybody developed/provided a basic file management UI for isolated
storage that I can cut and paste into my code with a conditional assembly
flag, or is there perhaps something that can run externally that can help?

Thanks


Peter Webb
(author of longest C# statement ever)
 
J

Jeroen Mostert

Peter said:
If Nicholas Paldino reads this, please don't have an apoplectic fit.

Other professionals with weak stomachs may want to look away.

My problem was that I needed to set up some files that the user can
read, write and delete. There are 20 of these, all xml, about 30k each.

I couldn't work out where I could read them from when the package is
first installed or run. Nor could I understand either of Mr Paldino's
suggestions.

So used Plan B. I wrote a small program to basically read in all each
xml files, map double quotes to single quote, slashes to pipes, and end
of lines to tildes, and write them out to a text file, preceded by
String Filename[counter]=" and terminated by ";
A few notes.

1. Prepending the string with @ (string s = @"...") will turn off the escape
mechanism, and then there is no need for escaping anything but the double
quote.

2. There's no need to include newlines at all unless whitespace is
significant, which it rarely is. If you want to include them for
readability, then you could use a quoted string, but the correct (i.e.
hassle-free quoting scheme) is:
- escape \ to \\;
- escape " to \";
- escape control characters (Char.IsControl) to their Unicode equivalents ().

No other quoting is necessary, nor is there a need to "reverse" quoting with
this approach. Actually, the last mechanism (convert to Unicode) can be
applied to *all* characters without thinking, at the cost of a ridiculous
sixfold increase in length.

3. Concatenating constant strings is done at compile time. Therefore, with
no loss of performance and a little extra effort you can break up the file
into multiple lines that don't have such a ridiculous length.

4. I've done this as well (large XML data as constant strings), although not
in production code.

5. A much, much better way of doing this would have been to include the XML
files as resources. It's in the MSDN. Don't be lazy.
 
J

Jeroen Mostert

Jeroen said:
Peter said:
If Nicholas Paldino reads this, please don't have an apoplectic fit.

Other professionals with weak stomachs may want to look away.

My problem was that I needed to set up some files that the user can
read, write and delete. There are 20 of these, all xml, about 30k each.

I couldn't work out where I could read them from when the package is
first installed or run. Nor could I understand either of Mr Paldino's
suggestions.

So used Plan B. I wrote a small program to basically read in all each
xml files, map double quotes to single quote, slashes to pipes, and
end of lines to tildes, and write them out to a text file, preceded by
String Filename[counter]=" and terminated by ";
A few notes.

1. Prepending the string with @ (string s = @"...") will turn off the
escape mechanism, and then there is no need for escaping anything but
the double quote.
And control characters that would break up the line, obviously.
2. There's no need to include newlines at all unless whitespace is
significant, which it rarely is. If you want to include them for
readability, then you could use a quoted string, but the correct (i.e.
hassle-free quoting scheme) is:
- escape \ to \\;
- escape " to \";
- escape control characters (Char.IsControl) to their Unicode
equivalents ().
Here "\uxxxx" is missing.
 
P

Paul E Collins

Peter Webb said:
This gave me a single valid line of C# code in a text file, which I
cut and paste into my source.
Its 623,060 characters long. [...]
So, how many of you so-called "experts" have ever written a line of C#
which is even 53k long, let alone 623,060 characters long? Huh?

Hmm, I remember once programmatically generating one huge Main method
with about a zillion variables declared in it, just to see what the
compiler would do. I think I got "unknown error".

Also, check this out:
http://equ.in/ox/comp/obfuscated/nasty_sql.txt
The resulting SQL expression (which is still in a few live databases, I
suspect) would have a good chance of reaching the Moon if you printed it
out.

Eq.
 
N

Nicholas Paldino [.NET/C# MVP]

Peter,

At first I was confused why you had singled me out, and why I would have
an apoplectic fit. However, reading the post, I believe I understand why.

For future reference, you should post the link to the previous thread
that you are referring to, and save me a few minutes of having to wade
through Google archives to figure out what the hell you are talking about.
=)

Ok, so the suggestion was to write the file that you needed, since you
couldn't get it to install with the ClickOnce application. The fact that
you are writing it to Isolated Storage is a red herring, as the issue is how
you store the contents of the file to write.

Again, I recommend using resources. Here are two links that give you
the "why" and the "how":

http://jasonhaley.com/articles/resourcefiles/resource files.htm
http://dotnetjunkies.com/WebLog/grant.killian/archive/2004/11/19/32776.aspx

Basically, in VS.NET, add a Resource file to your project (there is a
default one added to the Properties sub directory in some projects, which
becomes a new namespace). Double-click on the resource file and then copy
and paste your XML into the value column (the original XML, not the
translated one you are using) and add a name. OR, go to the drop down in
the upper left hand corner and select "Files" (it should default to strings)
and then drag the file with the XML to the designer.

When you do this, the designer will do a few things. First, it will
olace the contents in the resource file. Then, it will add a strongly typed
accessor to get the resource. If you copied and pasted the XML into a
string, then it will expose a property with the name specified in the
designer. This property will be of type string. If you dropped a file in,
then it will expose a property with the name of the file (sans extension) of
type byte.

Then, in code, you can access those static properties on a class with
the name of your resource file (if you added a resource file with the name
MyClass, then you can access the properties MyClass.MyFile, MyClass.MyXml,
assuming you dropped a file named MyFile.<any extension> or created a string
variable with the name MyXml.

As for ^when^ you do this, the recommendation was to check for the
existence of the file when the program is first run and then write the file
if it doesn't exist. I don't know if it is part of your application logic
to just write the file once, as it would seem that the application always
needs to have access to the file, so if that is the case, it should probably
write a default every time if it doesn't find one (so your app can work).

You can easily do this in the entry point for your app, or, a static
constructor on a type that will use the file first.

Isolated storage was mentioned by you initially in your post, and I
mentioned it again (although reading it, I don't believe I suggested it over
something else, I left that to you and your needs).

Isolated Storage is meant to be, well, isolated. If you need access to
the files that you write, then you should probably place the document in the
Application Data or Local Application Data directories. You can get these
directories through the static GetFolderPath method on the Environment
class, passing a value from the Environment.SpecialFolder enumeration
(either ApplicationData, if your user is roaming, or LocalApplicationData if
they are not).

If the data is not user-specific, and will be the same across all
running instances of the app, then you can place it in the directory
specified by the CommonApplicationData directory, but be careful here, as
you might not have write access to that directory, depending on the rights
of the user running it (I seem to remember on XP it was a subdirectory of
the Program Files directory).

In any of these cases, remember to create a separate sub-directory for
your files, and not dump the files directly into the directory returned by
the call to GetFolderPath.

As for your remark:
So, how many of you so-called "experts" have ever written a line of C#
which is even 53k long, let alone 623,060 characters long? Huh?

Well, I wouldn't even begin to consider someone an expert if they wrote
a single line of code 53k long.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Peter Webb said:
If Nicholas Paldino reads this, please don't have an apoplectic fit.

Other professionals with weak stomachs may want to look away.

My problem was that I needed to set up some files that the user can read,
write and delete. There are 20 of these, all xml, about 30k each.

I couldn't work out where I could read them from when the package is first
installed or run. Nor could I understand either of Mr Paldino's
suggestions.

So used Plan B. I wrote a small program to basically read in all each xml
files, map double quotes to single quote, slashes to pipes, and end of
lines to tildes, and write them out to a text file, preceded by String
Filename[counter]=" and terminated by ";

This gave me a single valid line of C# code in a text file, which I cut
and paste into my source.

Its 623,060 characters long. FWIW, it starts:

filename[0] = "Default configuration"; filecontents[0] = "<?xml
version='1.0' encoding='utf-8'?>~<saveStructure
xmlns:xsi='http:||www.w3.org|2001|XMLSchema-instance'
xmlns:xsd='http:||www.w3.org|2001|XMLSchema'>~ <alist>~ <textaslot>~
<clickarea>~ <Location>~ <X>404<|X>~

At runtime I just reverse the substitutions and write the 20 strings out
to 20 files with the appropriate names. Works a treat, and is virtually
instantaneous to do.

The IDE accepts this just fine. It does warn me that the program contains
an extremely long line (623,060 is extremely long?) and the UI may may be
slow, which it is, but only if the line is on the screen. I stick it at
the end after a bunch of blank lines so its no problem.

Now, a puritan may argue that this is actually 40 C# statements
concatenated together, which is true, but one of the String statements is
53k long, and unless there is something about carriage returns and strings
I don't know, cannot be any shorter.

So, how many of you so-called "experts" have ever written a line of C#
which is even 53k long, let alone 623,060 characters long? Huh?

Which brings me to my question about isolated storage.

Its working just fine for me, and thankyou to whoever suggested it. It did
seem to suffer from one problem, which is that it is only available to the
runtime. This meant I had to write some basic file management
functionality within my app for debugging purposes - clearing directories
and files, for example.

I don't want to have to do this again for the next project I decide to
undertake.

It seems that most people who use isolated storage to hold a file system
would have similar requirements during development.

Has anybody developed/provided a basic file management UI for isolated
storage that I can cut and paste into my code with a conditional assembly
flag, or is there perhaps something that can run externally that can help?

Thanks


Peter Webb
(author of longest C# statement ever)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top