C
C#•2mo ago
Steph le Fermier

Little problem with a function for a total newbie ...

hi guys ... I'm a C# newbie, just starting to learn and i am having a problem I can't seem to fix. i have a form with browse button, to select a directory in which i will look for some files. for example, i have a file named "map.i3d" and when found, declared as mapI3Dfile in my project. i have a function that is supposed to add 2 strings at precise places in the file in case of one or two don't exist. And if insertion of string1 is done correctly, a message is sent to a richTextBox. if no need to insert the string, another message is sent to the textbox. same thing for string2. the function does exactly what I want for string1, so I used the same "routine" to check and then eventually to insert string2. the insertion is perfectly done but the problem is pretty simple. No matter string2 is not already present in the file, the script thinks the string2 thinks the string2 is present, so it returns the bad insertion state. Here is below the code of the function that is handling that part... If someone can help me, that would be greatly appreciated !! Code of the function ===> https://pastebin.com/cnWUE051
Pastebin
` private void HandleI3DInsertion(string mapI3Dfile) ...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
59 Replies
reflectronic
reflectronic•2mo ago
it is hard to say anything without knowing what the file and what the tag are
Steph le Fermier
Steph le Fermier•2mo ago
would you need the whole code of Form1.cs ??
Steph le Fermier
Steph le Fermier•2mo ago
here is the complete actual code for Form1.cs : https://pastebin.com/QDtQxHVN
Pastebin
using System;using System.Collections.Generic;using System.Drawing;...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
reflectronic
reflectronic•2mo ago
no, not the form, the file you are reading and what tag2 is
Steph le Fermier
Steph le Fermier•2mo ago
ok ... the file is 190000+ lines xml file but I'll paste the part where the 2 tags are supposed to be inserted ... tag1="<!-- NEW FOLIAGES -->" tag2="<!-- END OF NEW FOLIAGES -->" Part of the code of the file : <?xml version="1.0" encoding="iso-8859-1"?> <i3D name="map.i3d" version="1.6" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://i3d.giants.ch/schema/i3d-1.6.xsd"> <Asset> <Export program="GIANTS Editor 64bit" version="9.0.4"/> </Asset> <Files> <File fileId="1829" filename="$data/fillPlanes/barley_diffuse.dds"/> <File fileId="1830" filename="$data/fillPlanes/barley_normal.dds"/> <File fileId="1831" filename="$data/fillPlanes/barley_specular.dds"/> <File fileId="1832" filename="$data/fillPlanes/canola_diffuse.dds"/> <File fileId="1833" filename="$data/fillPlanes/canola_normal.dds"/> <File fileId="1834" filename="$data/fillPlanes/canola_specular.dds"/> <File fileId="1687" filename="$data/fillPlanes/cotton_diffuse.png"/> <File fileId="1688" filename="$data/fillPlanes/cotton_normal.png"/> <File fileId="1689" filename="$data/fillPlanes/cotton_specular.png"/> I want to insert tag1 on the first line after the one containing "<Files>" and tag2 before the first one containing "<File fileId=" ... and then return a value to a variable so that the rest of the script will know what to write in the text box ... you see ?
reflectronic
reflectronic•2mo ago
you should really be using an XML reader for this, not string manipulations... it is much more difficult to get it right when you are doing it this way but, uh, i still don't understand your question
Steph le Fermier
Steph le Fermier•2mo ago
what u mean ? what do i need to clarify ?
reflectronic
reflectronic•2mo ago
i still don't really understand what doesn't work about your code
Steph le Fermier
Steph le Fermier•2mo ago
i'm a total beginner, i have to admit i used ChatGPT to build some sample code to understand the basics, but i can't find a way to get this crappy display to my textbox fixed ... with this Form1.cs everything works perfectly, except when tag2 is inserted, it returns the wrong insertion status code, so that the display to the textbox is not good ... you see ?
reflectronic
reflectronic•2mo ago
it would help to know what you are expecting to see and what you are actually seeing
Steph le Fermier
Steph le Fermier•2mo ago
the base file is the part i pasted above, and the same part with the tags inserted is : <?xml version="1.0" encoding="iso-8859-1"?> <i3D name="map.i3d" version="1.6" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://i3d.giants.ch/schema/i3d-1.6.xsd"> <Asset> <Export program="GIANTS Editor 64bit" version="9.0.4"/> </Asset> <Files> <!-- NEW FOLIAGES --> <!-- END OF NEW FOLIAGES --> <File fileId="1829" filename="$data/fillPlanes/barley_diffuse.dds"/> <File fileId="1830" filename="$data/fillPlanes/barley_normal.dds"/> <File fileId="1831" filename="$data/fillPlanes/barley_specular.dds"/> then in the textbox, if tag1 is inserted i want to see : "Insert I3D Tag 1 = OK", if tag2 is inserted I want to see "Insert I3D Tag 2 = OK"... if tag1 is already present in the file, then it should disply "Insert Tag 1 = N/A", and if tag2 is present, it should display "Insert I3D Tag 2 = N/A"
reflectronic
reflectronic•2mo ago
ok. sure. what makes you think it is not working, though
Steph le Fermier
Steph le Fermier•2mo ago
only the display in the textbox (kind of a log box) is wrong, the insertion works perfectly ... even if it is not present, and then inserted, status for tag2 is set to display N/A while i want that it displays OK i applied the same logic for insertion and status of tag1 and it works perfectly, can't figure out why it does not work the same way for tag2
reflectronic
reflectronic•2mo ago
well. it does not do that for me i have looked at your code, pasted it into my own project, made a test file, and I get
Insert I3D Tag 1 :
OK

Insert I3D Tag 2 :
OK
Insert I3D Tag 1 :
OK

Insert I3D Tag 2 :
OK
after passing it a file which contains this XML in it there is a problem, and it's that you code does not write the file correctly once i look at the file after it's written, i get this:
<Files>
<!-- END OF NEW FOLIAGES -->
<File fileId="1829" filename="$data/fillPlanes/barley_diffuse.dds"/>
<File fileId="1830" filename="$data/fillPlanes/barley_normal.dds"/>
<Files>
<!-- END OF NEW FOLIAGES -->
<File fileId="1829" filename="$data/fillPlanes/barley_diffuse.dds"/>
<File fileId="1830" filename="$data/fillPlanes/barley_normal.dds"/>
there should be a <!-- NEW FOLIAGES -->, presumably
Steph le Fermier
Steph le Fermier•2mo ago
i pasted the wrong code for the Form1.cs ... i'm so sorry 😢
Steph le Fermier
Steph le Fermier•2mo ago
here is the good version : https://pastebin.com/Bcccs7hR it is producing an error upon inserting tag2, but can't understand it
Pastebin
using System;using System.Collections.Generic;using System.Drawing;...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Steph le Fermier
Steph le Fermier•2mo ago
but even if it is producing an error message window, both tags are inserted correctly now this code needs to be fixed about that error message, and about the not correct status written in the textbox ... i'm pretty sure that it is a small small thing but as i'm a terrible newbie, i am totally unable to fix that thing
reflectronic
reflectronic•2mo ago
this code has the same behavior for me when i try it on this file:
<?xml version="1.0" encoding="iso-8859-1"?>

<i3D name="map.i3d" version="1.6" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://i3d.giants.ch/schema/i3d-1.6.xsd">
<Asset>
<Export program="GIANTS Editor 64bit" version="9.0.4"/>
</Asset>

<Files>
<File fileId="1829" filename="$data/fillPlanes/barley_diffuse.dds"/>
<File fileId="1830" filename="$data/fillPlanes/barley_normal.dds"/>
<File fileId="1831" filename="$data/fillPlanes/barley_specular.dds"/>
<?xml version="1.0" encoding="iso-8859-1"?>

<i3D name="map.i3d" version="1.6" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://i3d.giants.ch/schema/i3d-1.6.xsd">
<Asset>
<Export program="GIANTS Editor 64bit" version="9.0.4"/>
</Asset>

<Files>
<File fileId="1829" filename="$data/fillPlanes/barley_diffuse.dds"/>
<File fileId="1830" filename="$data/fillPlanes/barley_normal.dds"/>
<File fileId="1831" filename="$data/fillPlanes/barley_specular.dds"/>
it does not print an error, and it prints OK for both of them it still does not write the comments correctly i still get
<Files>
<!-- END OF NEW FOLIAGES -->
<File fileId="1829" filename="$data/fillPlanes/barley_diffuse.dds"/>
<File fileId="1830" filename="$data/fillPlanes/barley_normal.dds"/>
<Files>
<!-- END OF NEW FOLIAGES -->
<File fileId="1829" filename="$data/fillPlanes/barley_diffuse.dds"/>
<File fileId="1830" filename="$data/fillPlanes/barley_normal.dds"/>
without the starting comment then, only after i run it again, does it do what you say it does
Steph le Fermier
Steph le Fermier•2mo ago
even with the latest Form1.cs code i pastbined ?
reflectronic
reflectronic•2mo ago
yes the problem is pretty clear
Steph le Fermier
Steph le Fermier•2mo ago
huuuh
Steph le Fermier
Steph le Fermier•2mo ago
No description
Steph le Fermier
Steph le Fermier•2mo ago
the error i'm getting
reflectronic
reflectronic•2mo ago
yes, i also get this error
Steph le Fermier
Steph le Fermier•2mo ago
and the app window with the textbox ...
No description
reflectronic
reflectronic•2mo ago
only after i have already run it once, after it has messed up the file
Steph le Fermier
Steph le Fermier•2mo ago
huuuh so weird ... 😦
reflectronic
reflectronic•2mo ago
this makes complete sense your file is already broken you are re-running it on the broken file yes. this is expected. the fix is to not break the file
Steph le Fermier
Steph le Fermier•2mo ago
no my file, map.i3d, is not broken, i always reuse the orignal non modified file
reflectronic
reflectronic•2mo ago
your code modifies the file that you read from unless you are copying it over every time you try, it is the same one
Steph le Fermier
Steph le Fermier•2mo ago
any way that you can help me fixing all that mess ???
reflectronic
reflectronic•2mo ago
if (index != -1 && !lines.Any(line => line.Contains(tag2)))
{
List<string> updatedLines = new List<string>(lines);
updatedLines.Insert(index, tag2);
File.WriteAllLines(filePath, updatedLines);
}
if (index != -1 && !lines.Any(line => line.Contains(tag2)))
{
List<string> updatedLines = new List<string>(lines);
updatedLines.Insert(index, tag2);
File.WriteAllLines(filePath, updatedLines);
}
your lines here does not contain tag1 you are reading the file into lines then writing the new version of the file with tag1 then overwriting it, with only tag2
Steph le Fermier
Steph le Fermier•2mo ago
i have to admit that i did not write/produced this function ... i tried to but with my limited knowledge, i was not able to get any result i'm on that problem for the last 2 days, like 10-12hrs a day ...i think i have to stop here, forget a good reward from my school class, and use my tiny bash scripts that are way less nice looking
reflectronic
reflectronic•2mo ago
i would suggest changing this method to look like this:
string[] lines = File.ReadAllLines(filePath);
List<string> updatedLines = new List<string>(lines);

int index = updatedLines.FindIndex(line => line.Contains("<Files>"));

if (index != -1 && tag1 != null)
{
updatedLines.Insert(index + 1, tag1);
}

index = updatedLines.FindIndex(line => line.Contains("<File fileId="));

if (index != -1 && tag2 != null)
{
updatedLines.Insert(index, tag2);
}

File.WriteAllLines(filePath, updatedLines);
string[] lines = File.ReadAllLines(filePath);
List<string> updatedLines = new List<string>(lines);

int index = updatedLines.FindIndex(line => line.Contains("<Files>"));

if (index != -1 && tag1 != null)
{
updatedLines.Insert(index + 1, tag1);
}

index = updatedLines.FindIndex(line => line.Contains("<File fileId="));

if (index != -1 && tag2 != null)
{
updatedLines.Insert(index, tag2);
}

File.WriteAllLines(filePath, updatedLines);
this fixes the issue for me
Steph le Fermier
Steph le Fermier•2mo ago
which function ? HandleI3DInsertion ? Other ?
reflectronic
reflectronic•2mo ago
InsertTagBeforeLine the one that it looks similar to
Steph le Fermier
Steph le Fermier•2mo ago
that part should replace what i have in the "try { ... }" part , right ?
reflectronic
reflectronic•2mo ago
yes
Steph le Fermier
Steph le Fermier•2mo ago
you're right it has fixed the error window thing ... one good thing thanks ! but still reporting the wrong status code (i see N/A for both tags, while it should have been OK)
reflectronic
reflectronic•2mo ago
that's because your file already contains them
Steph le Fermier
Steph le Fermier•2mo ago
looks like the script scans the file AFTER it has added the tags
reflectronic
reflectronic•2mo ago
i am sorry but i do not believe you when you say you are using the blank files the code does not lie
Steph le Fermier
Steph le Fermier•2mo ago
i can swear on my mother's soul (gone 3 months ago) that I copy the original file over the modified/broken one everytime i try the app but could it happen like this if the scan is launched AFTER the insertions are done ? that would explain why it returns a "already present" state ...
reflectronic
reflectronic•2mo ago
ok. well, we can show this and it would not be too hard:
private void HandleI3DInsertion(string mapI3Dfile)
{
try
{
// Lecture de toutes les lignes du fichier mapI3Dfile
string[] lines = File.ReadAllLines(mapI3Dfile);

MessageBox.Show(string.Join("\n", lines));

private void HandleI3DInsertion(string mapI3Dfile)
{
try
{
// Lecture de toutes les lignes du fichier mapI3Dfile
string[] lines = File.ReadAllLines(mapI3Dfile);

MessageBox.Show(string.Join("\n", lines));

what does the message box say oh. i see what you mean sorry , i was thinking of the first code you sent
Steph le Fermier
Steph le Fermier•2mo ago
i tried the messagebox thing, the app is using like 100% of CPU and shows nothing ... normal ?
reflectronic
reflectronic•2mo ago
yes i see what is happening, it is true that it is doing the scan after the insertions are done in your first code it was not like that. well, kinda
Steph le Fermier
Steph le Fermier•2mo ago
yes, it was not the good one, i'm sorry for posting the wrong one at first
reflectronic
reflectronic•2mo ago
anyway, the issue is in goButton_Click
// Appel des fonctions pour insérer les tags dans les fichiers
InsertTagBeforeLine(mapI3Dfile[0], tag1, tag2);
HandleI3DInsertion(mapI3Dfile[0]);
// Appel des fonctions pour insérer les tags dans les fichiers
InsertTagBeforeLine(mapI3Dfile[0], tag1, tag2);
HandleI3DInsertion(mapI3Dfile[0]);
here, indeed, you are inserting the tags before you do the scan
Steph le Fermier
Steph le Fermier•2mo ago
so i have to "swap" those 2 lines ?
reflectronic
reflectronic•2mo ago
yes
Steph le Fermier
Steph le Fermier•2mo ago
ok let's try it it worked partly, the good reports status, but added the tags twice
reflectronic
reflectronic•2mo ago
oh. sorry. i missed part of your code
Steph le Fermier
Steph le Fermier•2mo ago
and if i click on Go a second time, it added the tags a third time, reporting it did not add anything
reflectronic
reflectronic•2mo ago
i did not see that you were still doing InsertTagBeforeLine inside of HandleI3DInsertion you need to delete InsertTagBeforeLine completely from goButton_Click
Steph le Fermier
Steph le Fermier•2mo ago
ok let's try it
Steph le Fermier
Steph le Fermier•2mo ago
it now works like a charm ... thank you so much !!! you saved my school exam ... you can't imagine how important it is for me to get this exam so that I would honor the memory of my mother !!
reflectronic
reflectronic•2mo ago
:-) i am glad that it is working for you
Steph le Fermier
Steph le Fermier•2mo ago
😉