c# and Unity, update records that have changed to a CSV file

故事扮演 提交于 2020-07-22 05:59:07

问题


I'm trying to work with a CSV file that is essentially a database of records. On load of the game I Read in a record and instantiate a prefab button, one at a time with the correct text from the CSV file. It's not always the case that the index of the button will be the same as the index of the record where I got the information in the CSV file. So far I setup a Save function that I want certain records that have changed based on a tricks "name" field to reflect in the CSV file. What would be an easy way accomplish this?

Here is the code that Saves changes to the CSV and Unity gives me an exception, IOException: Sharing violation on path:

void WriteCSVFile()
{
    StreamReader strReader = new StreamReader("Assets/Resources/characters.csv.txt");
    StreamWriter strWriter = new StreamWriter("Assets/Resources/characters.csv.txt");
        
    bool endOfFile = false;
    string header = strReader.ReadLine();
    strWriter.WriteLine(header);
    string data;
        
    while(!endOfFile)
    {
        data = strReader.ReadLine();
        strWriter.WriteLine(data);
        
        if(data == null)
        {
            endOfFile = true;
            break;
        }
        
        //dataValues is each row
        var dataValues = data.Split(',');
        
        if(dataValues[1] == trickName)
        {               
            dataValues[3] = currentXP.ToString();
            dataValues[4] = currentLevel.ToString();
            dataValues[5] = maximumXP.ToString();
            strWriter.WriteLine(dataValues[3]);
            strWriter.WriteLine(dataValues[4]);
            strWriter.WriteLine(dataValues[5]);             
        }
    }   
}

UPDATE: It seems to be working now but I need help with one more step. In the updated version of my save function you can see that I split each record by the commas into an array called dataValues. I make changes to the right fields in dataValues but now the text is separated into single lines when I do a Log statement. How do you think I can append the file back together to Write over the original CSV file? Here is the update to my save function: '''void WriteCSVFile(){

    string txt = System.IO.File.ReadAllText("Assets/Resources/characters.csv.txt");
    //StreamWriter strWriter = new StreamWriter("Assets/Resources/characters.csv.txt");
    
    string[] data = txt.Split(new char[] { '\n' });
    
    foreach(string record in data){
        var dataValues = record.Split(',');

        if(dataValues.Length >= 6 && dataValues[1] == trickName){
            Debug.Log(dataValues[1]);
            //dataValues[3] = currentXP.ToString();
            //dataValues[4] = currentLevel.ToString();
            //dataValues[5] = maximumXP.ToString();
        } 
    }
    
    '''

回答1:


In your code, you are assuming that every line contains at least 2 fields (by testing for dataValues[1] == trickName). Add a check for the length as well:

if (dataValues.Length >=6 && dataValues[1] == trickName)
{
   ...
}

Check for length (at least) 6, so that the use of dataValues[5] is valid.




回答2:


I recommend extracting your CSV file into an in-memory collection, then dispose reader to ensure it's no longer accessing the file. Loop through the in-memory collection and use the writer to update the appropriate values. Also, it's best practice place StreamReader and StreamWriter instances inside using statements to ensure they are disposed properly.



来源:https://stackoverflow.com/questions/62903645/c-sharp-and-unity-update-records-that-have-changed-to-a-csv-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!