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.
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 believe this is going to be a big question but I searched the web for at least an hour for an answer and I hope here I'll get the answer.
So the thing is I want to create my own file extension that will essentially be a binary file that my program will obviously will know how to interpret.
I know how to create a binary file and read from it, I understood that all I need is to change the extension and check it before I start reading the file.
The question is even if I create my own extension and check for it before start reading, I can't know that the file was really created by my application, someone could just change the extension and my application won't get the data it needs (of course I'll put everything in a try and catch so the application won't crush).
Is there a way to sign a binary file before opening it so people that get the file can know that my application created that file and not something that someone changed to look like my file type?
It's more of an idea I have so I don't have any code to show or any example other than this scenario.
All I need is something that people will know came from me and not something that every other person can generate on his own (not talking about programmers, I'm talking about regular people).
If you know anything about this subject please direct me to the answer or if you know post it here.
One way could be to encrypt the contents of the file, then have your application decrypt it.
Anyone opening the file in your application would immediately know if that application generated it.
I would create a special sequence at the beginning of the file (a file header)
I can't seem to do this without using a MySQL database.
I don't know how to store an array of data (ID Codes) inside of a file and be able to recall it.
I have 4,000 unique 4 digit codes that need to be stored in an array inside of a file. The machine the silverlight app will be running on will not have internet access so I can't create a database and use http calls or anything.. it needs to be local to the project. I also am looking for a way to then take said codes and be able to test against those codes ie pass user input and validate it against the array of codes.
I've looked all over and have no idea where to begin or even how to implement it. I'm not looking for or caring about processing power or anything.. I just need functionality.
you can store it in a txt file and attach the file to project as content.
the file will then be a part of the silverlight xap
read like you would read a content file.
a hashset would make perfect sense.. store however you want and load the data into hashset.. should get fastest lookups
HashSet
http://msdn.microsoft.com/en-us/library/bb359438(v=vs.95).aspx
I have a few questions. Yes this is homework and I am just trying to understand it.
This is what is being asked.
• When the button “Load” is clicked, read the file specified in the textbox (txtFilePath: Absolute path not relative) and add the objects found within to the listbox
• When the user clicks the “Save” button, write the selected record to the file specified in txtFilePath (absolute path not relative) without truncating the values currently inside
Can someone please explain to me as I am still learning this. I have the button and the textbox there and the same with the save. Now with the save button will I just have the same code as you would if you just wanted to save it. But from what I am gathering there is a database so you can load the file that you saved. Or am I making this harder than what it is?
No, no database. In these instructions, record == some selected item that needs to be appended to an existing file. Just use a stream and a writer to save the file to disk and you satisfy the requirement.
No, there is no database. What you do is interact with the Windows file system (eg, the files on your hard drive). You use the classes in the System.IO namespace to load and save files.
'Absolute path' refers to the unique location of a file in the drive expressed as a rooted expression; a 'relative path' is a partial path that points to a file relative to a given location:
c:\foo\bar\baz\my files\homework.txt
..\..\homework.txt
Those are an absolute and relative paths.
I'm not sure how much detail you are looking for here, it's hard to give a complete overview of the way filesystems work. You might want to look at the basic examples in MSDN that deal with file management.
It's hard to give a detailed analysis of this subject as it is quite a wide topic.
For file interaction you must use the System.IO namespace which has classes to easily load and save files.
http://msdn.microsoft.com/en-us/library/system.io.aspx
The link above is a good reference on MSDN on how you can get started with File Management using System.IO.
Good luck!
If I understand you correctly, your question is wether or not you need to read the file a second time before saving or otherwise treat if differently than if you created a new file.
I would say "no". You have already read the content of the file into the listbox. You just need to get the edited content from the listbox (when the user is done with it) and save it to the file (overwriting whatever is there).
First of all read up on how to read and write files. Here's a good link I found:
check it out
Next what you'll want to do is put your read/write code in the Button_Click event of each button (double click on your buttons to auto create this event assuming your using Visual Studio)
You can easily retrieve the path from your text box by accessing the .text() property of your textbox
string path = myTextBox.Text;
It's been a while since I've coded anything in c# but this is pretty basic and I think it should help.
For Load:
Read the file line by line
Add each line to the ListBox Items
For Save:
Open your save file without truncating (ie append to the file)
For each item in your ListBox Items, write it to the save file