Click here to Skip to main content
15,993,913 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using Visual Studio community edition 2017 and I am trying to create a script that adds a file, loops through the file line by line and find a certain symbols before some letters, for example ISA** change those symbols then save the whole file as a new text file. I am new to the visual studio and c# world and have no idea where to start. Would this just be a console app?

I think a stream is the correct way to accomplish this task but I just can't seem to find the right way to make this work.

What I have tried:

I have tried
string text = File.ReadAllText(@"C:path\to\file");
text = text.Replace("|","*");
File.WriteAllText(@"c:\EDITest.txt", text);


string data = File.ReadAllText("C:\path\to\file")
    .Replace("|", "*");
byte[] bytes = Encoding.ASCII.GetBytes(data);
Stream s = new MemoryStream(bytes);


I know that neither of these will complete the requirement but I keep getting errors. If someone could just point me in the correct direction to start this I would be so appreciative!!

thank you in advance for any comments or advice
Posted
Updated 16-Aug-18 10:35am

Start off by not trying to store data in the root directory of your C: drive - in modern OSes that folder is write protected (for security, so that viruses find it harder to change how your system starts up) so the chances are that one of your errors is an access violation exception, which is basically saying "You can't write to that file".

Instead, create a folder for your test data and set it's persmisions to "all users" (or for the more advanced version, see here: Where should I store my data?[^])

Other than that, the first version should work - if your path is valid, which the example probably isn't - try inserting a leading "\" to make the path absolute:
C#
string text = File.ReadAllText(@"C:\path\to\file");


If you are getting some other error(s) you need to explain exactly what they are!
 
Share this answer
 
Comments
Member 13935962 16-Aug-18 16:35pm    
thank you!!
You were right the error I getting was "unable to save at this location" basically...
I fixed that and am now able to save into a shared folder, so now I need to build on that with the ability to loop through the file and change the delimiters before the ISA and GS. So I basically have several files and they all have different delimiters so I need to make all the delimiters the same.
string text = File.ReadAllText(@"C:\Users\file\One\Desktop\path");
File.LoopFullPath
ISA_Postition =  InStr(linetext, "ISA")
ISA_Delimiter = InStr(linetext, ISA_Position+3, 1)
ISAstandardDel =  "~"

GS_Postition =  InStr(linetext, "GS")
GS_Delimiter = InStr(linetext, GS_Position-1, 1)
ISAstandardDel =  "\"

File.WriteAllText(@"C:\Users\test_shared_folder\DateTime.Now.txt", text);


any suggestions??

the current code gives me the following errors
LoopFullPath doesn't exist in the type "file"
InStr, linetext, ISAstandardDel ext... doesn't exist in the current context

do I add these as classes to define them??
 
Share this answer
 
v2
Comments
Patrice T 16-Aug-18 17:55pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.

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