Question by
SUPERDOOMPANDA · Sep 16, 2020 at 08:49 PM ·
filestream
Error writing CSV file with filestream
Hello, I'm trying to write a file, however, I've come up against this error which I'm a bit stuck on. I've noticed when I pass in a shorter string of text I don't get this error, however, this doesn't help me achieve the results I want for the CSV file. Any help would be greatly appreciated.
CODE:
//Copied from Datalog class (which passes the string values to the Write CSV class
public void WriteAdventureLog()
{
string txtHeader = "RUN,STEP,LOOP CYCLE,PLAYER LVL, PLAYER XP, S-CURRENCY, H-CURRENCY, ACTIVITY, STEPS, KM, INV COUNT, INV COMMON, INV UNCOMMON, INV EPIC, INV RARE, INV LEGENDARY";
WriteToCSV.AddRecordHeader(txt, "/inventory.csv");
}
//Copied from Write CSV Class
//First row is header for CSV
public static void AddRecordHeader(string text, string filepath)
{
try
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(filepath, true))
{
file.WriteLine(text);
Debug.Log("Got here");
}
}
catch (Exception ex)
{
throw new ApplicationException("Could not write to CSV file", ex);
}
}
ERROR:
System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) (at :0) System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.IO.FileOptions options, System.String msgPath, System.Boolean bFromProxy, System.Boolean useLongPath, System.Boolean checkHost) (at :0) (wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int,System.IO.FileOptions,string,bool,bool,bool) System.IO.StreamWriter.CreateFile (System.String path, System.Boolean append, System.Boolean checkHost) (at :0) System.IO.StreamWriter..ctor (System.String path, System.Boolean append, System.Text.Encoding encoding, System.Int32 bufferSize, System.Boolean checkHost) (at :0) System.IO.StreamWriter..ctor (System.String path, System.Boolean append, System.Text.Encoding encoding, System.Int32 bufferSize) (at :0) System.IO.StreamWriter..ctor (System.String path, System.Boolean append) (at :0) (wrapper remoting-invoke-with-check) System.IO.StreamWriter..ctor(string,bool) WriteToCSV.AddRecordHeader (System.String text, System.String filepath) (at Assets/Scripts/Utilities/WriteToCSV.cs:31) Rethrow as ApplicationException: Could not write to CSV file WriteToCSV.AddRecordHeader (System.String text, System.String filepath) (at Assets/Scripts/Utilities/WriteToCSV.cs:39) DataLog.WriteAdventureLog (System.String value) (at Assets/Scripts/Utilities/DataLog.cs:58) GUIController.DisplayAdventureLogText (System.Collections.Generic.List`1[T] items) (at Assets/Scripts/Systems/GUIController.cs:56) ChestSystem.WriteAdventureLog (System.Collections.Generic.List`1[T] lootItems) (at Assets/Scripts/Systems/ChestSystem.cs:101) ChestSystem.Encounter () (at Assets/Scripts/Systems/ChestSystem.cs:42) CoreLoop.CheckActionType (LoopAction loopAction) (at Assets/Scripts/Systems/CoreLoop.cs:92) CoreLoop.IntiateLoop (System.Int32 levelsToCycle) (at Assets/Scripts/Systems/CoreLoop.cs:61) SimEnvController.RunEnvironment (System.Int32 levelsToCycle) (at Assets/Scripts/Systems/SimEnvController.cs:23) SimEngine+d__13.MoveNext () (at Assets/Scripts/Systems/SimEngine.cs:58) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at /Users/builduser/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17) UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) SimEngine:RunSimulation() (at Assets/Scripts/Systems/SimEngine.cs:44) Button:RunSim() (at Assets/Scripts/Systems/Button.cs:23) UnityEngine.EventSystems.EventSystem:Update() (at /Applications/Unity/Hub/Editor/2020.1.1f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:376)UnauthorizedAccessException: Access to the path "/inventory.csv" is denied.
Comment