Click here to Skip to main content
15,886,075 members
Home / Discussions / C#
   

C#

 
GeneralRe: bind a gridview and a listbox to a database Pin
steven_noppe11-Jan-22 22:31
steven_noppe11-Jan-22 22:31 
GeneralRe: bind a gridview and a listbox to a database Pin
RobertSF12-Jan-22 12:37
professionalRobertSF12-Jan-22 12:37 
GeneralRe: bind a gridview and a listbox to a database Pin
RobertSF12-Jan-22 13:33
professionalRobertSF12-Jan-22 13:33 
QuestionIs there a way to improve this code? Pin
Richard A Knox4-Jan-22 8:06
Richard A Knox4-Jan-22 8:06 
AnswerRe: Is there a way to improve this code? Pin
Gerry Schmitz4-Jan-22 12:25
mveGerry Schmitz4-Jan-22 12:25 
AnswerRe: Is there a way to improve this code? Pin
Richard Deeming4-Jan-22 22:20
mveRichard Deeming4-Jan-22 22:20 
AnswerRe: Is there a way to improve this code? Pin
Pete O'Hanlon4-Jan-22 22:49
mvePete O'Hanlon4-Jan-22 22:49 
QuestionCSV File Access - Error Msg : the process cannot access the file used by another process Pin
The Golden Man4-Jan-22 7:26
The Golden Man4-Jan-22 7:26 
Hello Guys,

I have a CSV file which looks something like this :

Entry Price, Quantity, SL Level, TP Level, Side
nothing, nothing, nothing, nothing, nothing

As you can see, using the code below, I try to make sure if I have the file already, otherwise I create one with the same content above :

C#
if (!File.Exists(path))
{
    sw = File.AppendText(path);
    
    if (writeHeaders) {
    sw.WriteLine("Entry Price, " + "Quantity, " + "SL Level, " + "TP Level, " + "Side");
    writeHeaders = false;
}

sw.WriteLine("nothing" + "," + "nothing" + "," + "nothing" + "," + "nothing" + "," + "nothing");
sw.Close();

}


but the if a position is taking place, I want to replace "nothing" with real values, depends on each column :

C#
if (File.Exists(path) && PositionAccount.Quantity > 0)
{
    List<string> newLines = new List<string>();
    string[] existingLines = File.ReadAllLines(path, encode);
    
    foreach (string line in existingLines)
    {
        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);
    }

    File.WriteAllLines(path, newLines);
}


both above codes are working perfectly, and doing what they are supposed to do, EXCEPT THAT, when I try to READ the csv file from another strategy, it shows an Errors Message of "Error Msg : the process cannot access the file used by another process" and it disables everything.

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
C#
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.

C#
using (mystream = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{

}


Can you plz tell me how to fix it ? Thank you
AnswerRe: CSV File Access - Error Msg : the process cannot access the file used by another process Pin
Gerry Schmitz4-Jan-22 12:16
mveGerry Schmitz4-Jan-22 12:16 
QuestionAbout records Pin
Super Lloyd3-Jan-22 9:12
Super Lloyd3-Jan-22 9:12 
AnswerRe: About records Pin
OriginalGriff3-Jan-22 20:28
mveOriginalGriff3-Jan-22 20:28 
GeneralRe: About records Pin
Super Lloyd3-Jan-22 20:31
Super Lloyd3-Jan-22 20:31 
GeneralRe: About records Pin
Super Lloyd3-Jan-22 20:33
Super Lloyd3-Jan-22 20:33 
GeneralRe: About records Pin
Super Lloyd3-Jan-22 21:07
Super Lloyd3-Jan-22 21:07 
GeneralRe: About records Pin
BillWoodruff4-Jan-22 4:49
professionalBillWoodruff4-Jan-22 4:49 
AnswerRe: About records Pin
Richard Deeming3-Jan-22 23:14
mveRichard Deeming3-Jan-22 23:14 
GeneralRe: About records Pin
Super Lloyd3-Jan-22 23:17
Super Lloyd3-Jan-22 23:17 
AnswerRe: About records Pin
BillWoodruff4-Jan-22 5:38
professionalBillWoodruff4-Jan-22 5:38 
GeneralRe: About records Pin
Super Lloyd4-Jan-22 5:49
Super Lloyd4-Jan-22 5:49 
AnswerRe: About records Pin
endo funk7-Jan-22 1:55
endo funk7-Jan-22 1:55 
GeneralRe: About records Pin
Super Lloyd7-Jan-22 8:49
Super Lloyd7-Jan-22 8:49 
GeneralRe: About records Pin
endo funk7-Jan-22 10:39
endo funk7-Jan-22 10:39 
QuestionBlazor: IEnumerable ElementAt Pin
chrisb15162-Jan-22 23:49
chrisb15162-Jan-22 23:49 
AnswerRe: Blazor: IEnumerable ElementAt Pin
#realJSOP3-Jan-22 2:16
mve#realJSOP3-Jan-22 2:16 
GeneralRe: Blazor: IEnumerable ElementAt Pin
chrisb15163-Jan-22 10:11
chrisb15163-Jan-22 10:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.