开发者

CSV file writing ASP.NET concurrency issue

开发者 https://www.devze.com 2023-03-16 21:20 出处:网络
I have a web form in a .aspx form which takes a users details and then writes the details to a CSV file in C# located on the server. My question is how can I control multiple writes to the CSV file so

I have a web form in a .aspx form which takes a users details and then writes the details to a CSV file in C# located on the server. My question is how can I control multiple writes to the CSV file so the data doesn't get messed up? I must absolutely use a CSV file on the server, a database or other ways to record it is not an option for me.

Looking around I have seen that using a Mutex may be useful. I was wondering what other options I have as I am looking for the most efficient and simplest solution? I apologise in advance as I am new to开发者_StackOverflow中文版 ASP.NET and C#.


Using a mutex/lock is the easiest way; assuming you site is the only writer of the csv file. Since no code is shown, I'll assume you have CsvWriter class:

public class CsvWriter
{
    const string path = "myfile.csv";

    static object synch = new Object();

    public void AppendCsv(string newData)
    {
        lock (synch)
        {
            File.AppendAllText(path, newData);
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消