Crashed when adding a really long line of code - c#

I am using VS 2010 express for C#.
I am making a random chinese name generator.
I have nearly 200 thousands first name to be drawn.
When I add a really long code with these first names like
string[] firstname = new string[] {"A","B",...}
VS was in heavy load when I opened the project or attempted to click on the line. Actually, after adding the line, I cant even open the project.
I guess it is due to the auto-debugging function as it is not so laggy when I try to edit in notepad++.
Any recommendation on handling the problem of drawing a large set of elements?
Or how can I stop the auto-debugging function?

string[] firstname =
System.IO.File.ReadAllLines("list-of-names-one-per-line.txt");
You should really consider reading from some kind of storage. This will also allow you to change the word list without recompiling your program.
If you must, Tools->Options->Text Editor->C#->Advanced uncheck Show live semantic errors

Read the values from a file. If you want to ensure the contents are included with an executable, then embed the file as a resource in the project and read the contents from there.

Related

EntityFramework not generating C# files properly (some enums are incomplete, so build fails)

At work I just installed a brand new copy of my OS and a brand new copy of VS2015. When I clone my solution for the first time, I cannot build it anymore, even if I've generated the C# files like I always do, by opening the .edmx file first, and clicking on "save" icon.
When building it throws the error:
CS0150: A constant value is expected
Because the enums that it has generated are incomplete! An example of one:
public enum DocumentType : int
{
Identity = 1,
ResidenceProof = 2,
RegisterDoc = ,
}
I also had this compiler error at the time, but after fixing it my C# enums are still being generated wrongly:
The ADO.NET provider with invariant name 'MySql.Data.MySqlClient' is either not registered in the machine or application config file, or could not be loaded. See the inner exception for details
How the hell should I fix this problem?
I had the same problem. Turned out that texttransform.exe cannot understand different line endings well. My .tt file was saved with Unix EOL, and when I saved it with Windows EOL, it started to work correctly. Just that simple - open your .tt file in WordPad and save.
Not really an answer but i'll post my findings and the workaround i chose to use;
The T4 Code Generation template (The file attached to your .edmx file using the .tt file-extention) contains code to generate C# using the data available in your model, I suspect the code at line #204 (might be located on a different line number in your version) to contain a minor bug.
A screenshot from my active project and solution explorer:
This line causes the faulty enums:
this.GenerationEnvironment.Remove(this.GenerationEnvironment.Length - 3, 1);
This presumably removes generated code characters that were added by the enum generator in order to remove the last , from the enum, as it is unnecessary.
I tested the functionality of this by adding output in front of this line, i tried creating an enum that would output MyEnumMemberName = TEST, and found that the output contained MyEnumMemberName = TES,.
Using this I found that changing the line to:
this.GenerationEnvironment.Remove(this.GenerationEnvironment.Length - 2, 1);
solves my issue.
Can't currently check if this also works on the machines that were already generating correct code but I hope it helps some of you. Good luck.

C# Change File Location For Next Time Program Runs

I am relatively new to C#, however I do have some basic knowledge of code from courses in high school and university. However, there is one thing I have not been able to figure out over the years. I am currently making a Form Application for a database system that stores information in a List using Visual Studios 2010.
On my main form; when the save button is pressed, the information is then serialized into an XML file. When the information is loaded, the information is then deserialized and put into the List for use in the code. All this is working correctly.
This process of saving and loading is done based on a string which contains the file path. This string is the location of a folder on my desktop (I put it there for easy access), and I am able to change the string in the code to basically move where the information is stored.
However, I have a separate "Admin" form which is able to change this file path string. When the user clicks the button to change the file path, I get the input from a text box, check its formatting, move the current file to the new location and update the location for the save method so changes can be saved before the program is closed. From there, the program reacts the same way as if I had changed the string from inside the code.
The problem occurs when I close the program. I do not know how to tell the program when it runs again that the location has been changed from the default and look for the file in the new location. The program reacts just like the file was missing (like it should) when it looks in the default location.
So basically, how do I tell the program that the save location was changed from when it was last run so it knows to load the info from a new location?
I have tried looking for an answer since high school (about 2 years ago) and have not found a solution. As a result I usually just keep the save location as the default (which I set it to) and don't try to change it. But this time, its important that the save location can be customized. My experience with Visual Studios is limited, as everything I know is from messing around with the program and looking up stuff when needed.
If needed, I can post snippets of my code. Thank you in advance!
It seems like what you really want is to save some user-defined settings for recall at run-time. Here is a MSDN link describing some basic conventions for storing / retrieving these settings.
https://msdn.microsoft.com/en-us/library/bb397750(v=vs.110).aspx
A *.config file would suffice (depending on the scale of the application).
Otherwise, you may want to go down the route of storing these settings in a database (if the scale is rather large, or if user-authentication is required for the application).
Here is another previous question dealing with this same subject (regarding App.config files):
What is App.config in C#.NET? How to use it?
I recommend using a config file where the .exe is, and write the location there, then read it in on program startup.
In particular .net provides this class which can manage your config file for you (assuming you have an app.config in your solution, otherwise create one)
https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings(v=vs.110).aspx

Checking source file and automatically generating resx file

Hello everyone here is my problem. I have the source file of a web page and it will be translated into different languages, so there are lots of meta:resourcekey keywords everywhere as you can imagine.
What I want is a plugin in VS that will first create a resx file and then check the whole code and whenever it sees the "meta:resourcekey" it will add a new string in the resx and copy whatever it sees on the right of "meta:resourcekey"
Can anyone help?

Read a specific line in a file

I know this has been answered several times before. But I am asking this for C#. Whereas there were a few for java. There was another one for C#, but it got me nowhere. So please do not rate poorly as I have a reason for this. Thank you.
My ultimate goal is to have a settings feature in my application. And I want all these settings to be saved into a text file. And when the application opens, it will automatically read that file and adjust the controls on my form.
I am unsure of where to start, but I was wondering if there was something along the lines of
String readLine(File file, int lineNumber)
Thank you in advance.
I already have a file being saved, and a file being opened. But I only have one setting saved in there. And that takes the first line. But if I want to add more controls being saved, rather than making a new file per option, I'd like to place several lines in that file, and each line would have its own control that it would change.
So, how can I read a specific line of a text file?
Thanks once again.
Principally one must read each line of a text file to locate a specific line (stopping at that line if desired) because each text line can be of a different length, so you can't just compute an offset in the file and go there.
If this really is a configuration file (which presumably isn't huge, you could just use
string[] lines = File.ReadAllLines(myPath);
http://msdn.microsoft.com/en-us/library/s2tte0y1.aspx
That way all of the lines of the file are read into lines and you can access an individual line like
string line = lines[2];
If you really only want to read a specific line (keep in mind this will be very inefficient if you read multiple lines over time in your app because you keep re-reading from the start of the file), you will have to write your own helper routine.
If you want a settings feature use the default and don't roll your own. You will only run into more issues with parsing and upgrade and finding a place to save it so that it can be modified when users are running under UAC or non admin accounts
Unless all the lines are exactly the same length, you can't just skip to a certain line number in the file. If you'll be needing most of the options, just read the file as an array of lines using System.IO.File.ReadAllLines:
string[] lines = File.ReadAllLines("myfile.txt");
Then you can just access each line like you would normally, say lines[1] for the second line.
Also, if you don't need to modify these settings with anything else, just use the built-in settings. They handle everything for you.

Visual studio 2010 empties the file on crash

I got a really bad problem while working on visual studio 2010. Accidently the power plug switched off and when I started the computer again the file was completly empty. I tried out following things:
I opened it in notepad and other couple of editors and it was empty.
I then opened it in Hex Editor. Hex editor shows that all bytes are set to 0.
I programatically read the file and it also showd all bytes set to 0.
Checked "Documents\Visual Studio 2010\Backup Files\" for my project and it was empty.
The file size is still showing in KBs but the code is completly gone.
Is there any possible way by which I can recover my code?
If there is not, can anyone suggest me a setting/patch taht should be there so that it never happens again.
Note: I already have Autorecover option set for every 5 minutes in IDE.
Update:
As suggested by Henok, If you have compiled and built the code at least once, you can reverse engineer the binary through reflector.
Doesn't look like it, to stop in future though, save and save often. Also look at using subversion like svn, or Git.
IIS has DLLs cached under C:\Windows\Microsoft.NET\v4.0.xyz\Temporary ASP.NET Files. Look for the dll and use a reflector. I use ILSpy.
Save often and use source control. I use C.V.S., personally.
It sounds like the I.D.E. had the file(s) open for writing at byte 0 when the computer went down, clearing everything out.
Beyond your software problems, I suggest you manage your power plug in such a way that it won't be accidentally switched off.
Same thing happened to me and thought I would post it here for those who would come here for answers.
If you have compiled and built the code at least once, you can reverse engineer the binary. Reflector did the trick for me.
Visual Studio still makes source files empty on sudden crashes, so I think I should share my solution.
Use any cloud file syncing service that supports file versions (for deleted files, too). Dropbox and Google Drive is what I can name. I randomly preferred Google Drive, though Dropbox can do all the same things.
I simply put my source tree in Google Drive, because it has file versions. My builds happen in Google Drive too, so there's much of unwanted traffic for big projects, but you can exclude some subfolders from syncing.
The drawback is that sometimes (in rare cases) Google Drive locks files and Visual Studio pops up "Save As..." dialog or some messages. You can usually close it, then save again successfuly. In very rare cases I had "The file is used by process" errors, and I had to restart Google Drive.

Categories