How to refresh text from a text file [duplicate] - c#

This question already has answers here:
How to detect real-time change of text files?
(3 answers)
Closed 1 year ago.
I am trying to make a chat program which reads text from a text file on a shared folder.
The problem I am having is when you type stuff it doesn't refresh meaning if someone else adds anything else to the text file you wont see it does anyone have an idea of how to fix this.
(this is a console program and I am new to c#)

You will need to either constantly be reading from the text file and have some way to detect changes. Otherwise I would seriously suggest not to use a text file for this kind of application.
Ideally you would want to be able to directly make a push to the application when new data is added. It might be worth researching into Socket Programming.

As #TomBowen suggested, text files should not be used for this purpose. However, for learning purposes, we can oversee that. 😉
I would suggest caching the path to your shared file. Have a loop (preferably async) that would constantly check when the file is updated and save the modified date locally. Do this by checking the last time the file was modified (How do I get modified date from file in C# on Windows Mobile?). If the modification date does not match your saved one - clear the console, read all the text file's content and print it to the console.
This would be a very primitive approach with tons of possibilities for stuff to fail. But it would work just for the sake of making something.

Related

Remembering the last position in a .txt file in C#?

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

C# Win32 Fire event on file read

I want to be able to detect if a file is read by another process. For example, if a text editor open's a file, I want to have a function to be able to get the name of the file that was read.
I saw that you can use the FileSystemWatcher class in C# to watch a directory for file changes but they don't solve the problem, I saw these on a 6 year old thread.
Has anything new been added to C# or Win32's API since then to allow file open detection?
Indeed this is a very complex subject, you would need to work with a File System Filter Driver (https://msdn.microsoft.com/windows/hardware/drivers/ifs/index) very low level stuff which would be hard to implement, I think that at least in the .net world there is not an easy way to do this.

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

Creating a custom file property in C# [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Can one add custom properties to NTFS folders?
I am trying to write a standalone executable in C# that attaches a value to a text file (without changing the content of the file) which can be read later.
I have looked into the following solutions but none of them quite work:
Shell32.dll: Only appears to allow reading the properties. I could not find any information on how to use Shell32 to write properties.
DSOFile: App needs to be standalone and I am also concerned about future OS compatibility.
NTFS Alternate Data Streams (ADS): Many tools will not copy this information if the file is moved or copied and I am concerned about future compatibility.
Is there another way (COM, Etc..) to attach a custom property to a text file in .NET?
The only three ways I can think of:
ADS - however you've already dismissed this.
Add it to the file name.
Add it to the beginning or end of the file.
If it's an application controlled file that may be moved around then your best bet is number two. Of course, this means that the file may "lose" that info in a renaming operation.
The third option is a bit iffy. If you can control how the file is viewed, then you can simply give it a different extension and write your data to the top of it. When it needs to be opened you can "extract" the real file and open accordingly. I think this is WAY more complicated... but it will contain your data and be less likely to be jacked with.

Saving and reading from specific parts of a .txt file

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.

Categories