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.
Related
I found myself being stuck on a certain problem. Currently I am writing a program in C#, that reads in .txts. These .txts are constantly changing, as in content is being added to them.
I created a FileSystemWatcher, that hands the name of the file to the method reading in my txt. Now I would like to have something in between, that remembers the last position I read in the txt and then only passes on the new part.
It also needs to remember it the next time I start the program.
I thought about storing the last line in a xml or txt and then letting my method search that file for the last line noted there.
The only similar question I found was this: Read log file from last read position . I don't really understand it though.
Is there anyway how to do this more efficient or elegant?
Edit: I already linked the suggested question and no, it is not similar to mine. I am looking to permanently store the information on how far I am into a .txt, not just on runtime.
When you need to store a finite amount of information on program state on the Windows platform in between runtimes, the Windows Registry is usually the way to go. Here's a stackoverflow question that will show you how. Use HKEY_CURRENT_USER so that you don't have UAC/Admin Rights issues:
Writing to registry in a C# application
.NET 4.0
Will this help? One way could be to store the line number instead of storing the content of the last line read.
Since .NET 4.0, it is possible to access a single line of a file directly. For instance, to access line 15:
string line = File.ReadLines(FileName).Skip(14).Take(1).First();
This will return only the line required
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
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.
Aspiring programmer here!
You can skip this part if you find it irrelevant.
Im currently working on a project where i give every program a username and i let people edit their own part of of each file. This will work more or less like onenote (great program), but i want to be able to restrict access to certian parts of it. I also need the file to be stored locally (on a shared location that is). In time i also want the files to be encrypted, but ill stick with the editing part of my question for now.
So my real problem:
I need people to be able to edit their part of a .txt file. The setup will probably look like this:
Document name:
Task 1: (Task name, like "Write a paragraph about Einstein"
User 1: (User 1s answer)
User 2: (User 2s answer and etc.)
Then ill follow up with more tasks / users. What i need to make my program understand is to read-only document name and other users answer, and read-write on their own part of the file.
What code can i use to accomplish this?
You won't be able to do that in a text file. You'll have to spend a little bit more effort in solving that task. Use a database and manage those tasks in a table and the user responses in another table. That will shurely work much better, trust me.
If you really want to use a text based solution for this instead of a database I would look into XML files.
They give you the ability to structure a document and have capabilities of being queried by C# trough XPath or Linq to XML queries.
This documentation should get you started.
Yea a database (use an MSAccess MDB file if you don't have a SQL server) would be best for this.
However, if you really want to use a text file, what you do is
1. Use System.IO to read the file and parse the file to get the piece of data you want them to edit (only read it out of the file, don't modify the file at this point).
2. Display the piece of data to them in a textbox or whatever.
3. After they've mdofied it, then read in the whole file into a variable
4. replace the old piece of data with the new data. in the variable.
5. output the entire variable back into the file and save and close it.
If this is REALLY what you want to do, i can post some code for you.
I want to execute a cygwin command from within a webservice.
Basically I want to use the "tail" command to strip off the first line of a file in C#.
Calling another program just to strip the first line of a file sounds like a very bad idea. You might want to try and just strip the first line in C#.
I've not personally dealt with huge text files before, so I did a bit of searching around;
Efficient way to delete a line from a text file
Basically, this one gives an answer you don't like, but if .NET 4 is an option memory-mapped files might help you out.
Are you looking to remove it or read it? If you want the first line of the file, you can just open the file stream (File.Open) and take the first line.
Normally Cygwin is installed in C:\CYGWIN so you should be able to run tail (from /usr/bin) by calling "C:\cygwin\usr\bin\tail.exe" from your code.
That said, you really should not be doing this at all. Just use a StreamReader properly. This question has a nice example to show how: Reading large text files with streams in C#