Click here to Skip to main content
15,885,869 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have this text file structured like this:
ID1|PROJECT1|STATION1|LOCKED
ID2|PROJECT2|STATION2|RELEASED
ID3|PROJECT3|STATION3|RELEASED
ID4|PROJECT4|STATION1|LOCKED

What i want to do is to: check if file exists, if not create it and add the data in it(done), else if it already exists see if some ID and PROJECT on a specific station is released or locked if it is released do something and if it's locked update the file and make it released, AND if non of the above add ID|PROJECT|STATION|RELEASED in the file. I need to do the update on existing ID|PROJECT|STATION without creating any duplicates in the file and adding them only once. I've tried a few things but none seem to work. Any suggestion on how i could update existing lines in file without creating duplicates and if the line does not exists in file to add it?
Posted

There is no automatic way to do that - text files aren't structured so there is no "helper" software you can really use.
You can do it fairly easily: just read all the lines into a string array, split each on the '|' character and check the individual sections against the numbers you want. Then modify the appropriate line, and write them all back. But it's a bit cumbersome - if you can change the file format, then using XML would let you treat it more like a database and update only the "row" you are interested in. (It would basically do the read-modify-write stuff for you behind the scenes) Depends I guess on the likely number of IDs and Projects you expect to handle.
 
Share this answer
 
Comments
Ioan-Alexandru 13-Aug-13 4:28am    
is it right if i add the info in the file line by line in a list of strings? and then check if the new project i want to add already exists or not, if not add it to the list and then rewrite the whole file? and if it does check if it's relseased or locked, if released post message if locked replace the work locked with released and rewrite the whole text file?
OriginalGriff 13-Aug-13 4:32am    
Your use of "locked" and "released" implies that there may be several users accessing the same file - is that correct? Because if it is, you need a better solution - text files do spectacularly badly with multi user access.
Ioan-Alexandru 13-Aug-13 4:36am    
no that's why i'm not using xml file, it's just like a simple log that i want to have on my station. but i ca't figure how to check the file see if what i want't to add new exists or not, if it does exists if is locked or released in case it's locked make it released and if relsead just show a message box, AND if it doess not exists to add it and then close the file.
OriginalGriff 13-Aug-13 5:13am    
OK - a text file you can't just "replace" a line: you really should rewrite the whole file as text files don't actually understand lines, it's the software that processes them that does that.
So, read all the lines (trivial: File.ReadAllLines will do it) loop through them looking for the one you want (using string.Split on '|' to make it easier) then replace the line in the array and write it back - or display your message box.

Is part of this difficult for you? If so, tell me which, and I'll see if I can be more detailed! :laugh:
Ioan-Alexandru 13-Aug-13 5:21am    
i think i'll manage thanks a lot for advice and help:D i i have any more problems i'll ask :D for now it suffices:D
You should parse the input file for getting the relevant data and store it in a suitable data structure. You may, for instance use a dictionary of tuples (see this Stack Overflow question: Composite Key Dictionary[^]) it would automatically handle the uniqueness requirement for you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900