How to write to a file with a relative path

Hello everyone!
I’m making a sort of gpu benchmark for a school project in unity. When it’s done executing, it should write a result in a file.
I first tried giving it a relative path, such as “…/Benchmark/score.json”, but I realised it only created or updated the file when I was playing the scene from inside unity, or clicking Build&Run. If I ran the executable from the project folder, everything worked well except the file wasn’t created. If I give it an absolute path, like “D:/score.json” , the executable creates the file as well, so it works as intended.
Is there any way I can modify the script, so that the path is relative but it still works?
Thanks in advance! :slight_smile:

1 Like

Sir, Application.dataPath; gives me Assets folder path like

Path : C:/Users/kp35511/Desktop/KayumiyWorking/MultiExeProgram/Assets

I need a path until project folder not assets folder which is one step before

Path : C:/Users/kp35511/Desktop/KayumiyWorking/MultiExeProgram

You should write data only into the persistent data path:
https://docs.unity3d.com/ScriptReference/Application-persistentDataPath.html

1 Like

I did like this way;

path+ "/Builds/SystemMonitor/MultiExeProgram.exe"```

It’s up to you, but be prepared that it may not work for all platforms.
Please next time do not duplicate your question.

1 Like

I am Sorry, it will happen again. I just wanted to find solution in short time.
I will never duplicate again.

I have one more question according to your message.

I try to open Unity execution file (.exe) from script. As you know, Unity execution file is in the Builds folder which are out of asset. I need a path to the Unity execution file in order to open .exe file.

I using this code below

 Process.Start(@"path")

Do you think this is the right way to access my Unity execution file.

Are you talking about run time or in the editor? What is the exact behavior you’re aiming for?

run time

Well, you’re already running the executable, so I don’t understand why do you want to launch another one. But in this case the dataPath is your closest you can get.
https://docs.unity3d.com/ScriptReference/Application-dataPath.html

But again, Unity wasn’t built for this.

1 Like

If you don’t mind, can you answer here

https://forum.unity.com/threads/open-multiple-unity-external-exe-file-from-a-script.722333/

my original question was another thread which was created by me.

you said that don’t duplicate your question, So I gave you link showing my original question

I always use persistentDataPath for external data, and then use (even though it’s not recommended by the Unity creators in the documentation unless they changed it) Resources.Load() to load all of my internal assets. This ensures your game is compatible to deploy with most all platforms.

If you’re creating a game in where your user can mod stuff, you want to put it all in the persistentDataPath folder.

System.IO is copmatible with Android, I’m not sure about iOS, but you’ll only be able to find your files if you use persistentDataPath.

What you should do for save games is use an encryption method to Encrypt() and Decrypt() your data file so that people can’t mess around with it.

If you want things moddable look at a game like 7 Days to Die also and how they do it, I thought they built that game with Unity. Almost everything works using XML files.