I have a CSV file which looks something like this :
| Entry Price | Quantity | SL Level | TP Level | Side |
| nothing | nothing | nothing | nothing | nothing |
if (!File.Exists(path))
{[INDENT]sw = File.AppendText(path);
if (writeHeaders) {
sw.WriteLine("Entry Price, " + "Quantity, " + "SL Level, " + "TP Level, " + "Side");
writeHeaders = false;[/INDENT]
}
sw.WriteLine("nothing" + "," + "nothing" + "," + "nothing" + "," + "nothing" + "," + "nothing");
sw.Close();
}
if (File.Exists(path) && PositionAccount.Quantity > 0)
{[INDENT]List<string> newLines = new List<string>();
string[] existingLines = File.ReadAllLines(path, encode);
foreach (string line in existingLines)
{[/INDENT][INDENT=2]string[] columns = line.Split(new char[] {','});
string EntryPrice = columns[0];
string Qtty = columns[1];
string SSLevel = columns[2];
string TTPLevel = columns[3];
string PSide = columns[4];
EntryPrice = EntryPrice.Replace("nothing", PositionAccount.AveragePrice.ToString());
Qtty = Qtty.Replace("nothing", QtyMini.ToString());
SSLevel = SSLevel.Replace("nothing", LongSL.ToString());
TTPLevel = TTPLevel.Replace("nothing", LongTP.ToString());
PSide = PSide.Replace("nothing", "Long");
columns[0] = EntryPrice;
columns[1] = Qtty;
columns[2] = SSLevel;
columns[3] = TTPLevel;
columns[4] = PSide;
string newLine = string.Join(",", columns);
newLines.Add(newLine);[/INDENT][INDENT]}
File.WriteAllLines(path, newLines);[/INDENT]
}
I really tried all possible ways that I came across in all forums to fix this problem, but I was not able to do it, I think that the file is not closed from the first opening this is why it shows this message, so I tried using (), I tried FileSharing, File Access, I tried FileStream, I tried StreamReader, but in some of them I was not able to readalllines and writealllines, as well I was not able to read only line[1] for example, and modify it and replace it.
using (mystream = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
}

Comment