- Home /
Write to text file in folder of executable
im trying to use system.io to write to a text file. That is working.. but i want to make it write to the file in the folder of the executable.. so instead of having to put a full file path it will just check the root folder dynamically.
if (System.IO.File.Exists(@"\text.txt")) {
System.IO.File.AppendAllText(@"\text.txt", "my string...");
}
this doesnt work.. obvisouly. How can I make it work?
that directs into the _Data folder not the folder of the exe and its dependencies.. the _Data folder throws sharing errors..
Answer by MinecraftProgramming · Oct 10, 2016 at 01:01 PM
@thornekey My answer is probably way to late, but I wanted to do the same thing and ended up doing it so:
string file=Application.dataPath;
string[] pathArray = file.Split('/');
file="";
for(int i =0;i<pathArray.Length-1;i++){
file+=pathArray[i]+"/";
}
file+="data.json";
It's probably not the best way, but it works.
Answer by Kiwasi · Feb 28, 2015 at 10:07 AM
Typically this is bad practice. Most operating systems want you to write in the documents folder (or equivalent). This can be accessed with Application.persistiantDataPath.
However if you know what you are doing then check Application.dataPath will work.
@Bored$$anonymous$$ormon the issue with persistantDataPath is that its hidden away in AppData, I was the general use to be able to access it in the same folder as the application - whereever it may be. and dataPath wont work as its protected in the _data folder..
Fair enough. Can you simply parse the path to remove the _data folder? $$anonymous$$y understanding of .exe builds was they had to be in the same folder as the _data directory.
ok Ive split the string and it i get a weird error by the document when i open it.. it says "The process cannot access the file because it is being used by another process" is there a way to close my game from using it?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Move files from folder a to folder b 1 Answer
Making a bubble level (not a game but work tool) 1 Answer
Saving File from "RAM" string array only saving one file 0 Answers