- Home /
Can I get a folder up hierarchy relative to the dataPath?
So I have something like this:
var mpDataPath = Application.dataPath;
var files = Directory.GetFiles(mpDataPath);
but I'd like to NOT have my data files in Assets at runtime (the keep getting imported into Unity authoring) and NOT in my _Data folder at runtime, but maybe up one level.
Are there any symbols I can use to indicate "up one folder level"?
Does Unity allow this if I for example just edit the dataPath string to represent the folder up one level.
My folders might be look this:
application.exe
application_Data
mpData
data files...
Answer by duck · Feb 19, 2010 at 10:06 PM
Even better than a symbol, the "Directory" class has a function to do just this called "Parent". Check it out on MSDN here.
And to get a fuller understanding of the use of files and paths in unity, using .net's FileIO, check out these pages:
(even if you're using javascript, the c# reference will be useful, since all the functions and variable names for the .net classes are exactly the same!)
Also if you're using Javascript, switch the syntax to JScript in the $$anonymous$$SDN docs, and that will almost always be identical to what you use in Unity. They typically don't have JScript example code though.
Answer by Eric5h5 · Feb 19, 2010 at 10:14 PM
If you want to use symbols, ../ is the usual way.
var oneLevelUp = Application.dataPath + "/../";
var dirInfo = new System.IO.DirectoryInfo(oneLevelUp).GetFiles();
for (fileName in dirInfo) {
print (fileName);
}
Huh, I had tried that initially, but maybe I messed it up in some way...
Answer by JDonavan 1 · Feb 19, 2010 at 10:27 PM
This is a horrible hack but it works. At least from run mode in the IDE...
using UnityEngine; using System.Collections; using System.IO;
public class TestPath : MonoBehaviour {
// Use this for initialization
void Start () {
string assetsPath = Application.dataPath;
string configPath = assetsPath.Substring(0,assetsPath.LastIndexOf('/')) + "/config";
string[] files;
string input;
StreamReader reader;
Debug.Log(Application.dataPath);
Debug.Log(configPath);
files = Directory.GetFiles(configPath);
foreach (string fileName in files)
{
reader = File.OpenText(fileName);
Debug.Log(fileName);
while ((input = reader.ReadLine()) != null)
{
Debug.Log(input);
}
reader.Close() ;
}
}
// Update is called once per frame
void Update () {
}
}
The "LastIndexOf" is part is actually cool. Where does that function come from. I haven't seen many string functions in the Unity Scripting Refrence
You won't find any of the $$anonymous$$ono/.NET functions in the Unity scripting reference. Use the $$anonymous$$ono or $$anonymous$$SDN docs for that.
Answer by davedev · Mar 13, 2010 at 09:00 PM
Not the exact answer, but related, I recently found the Path class in Unity which has functions for manipulating paths like GetDirectoryName, GetFileName.
Answer by pavlito · Oct 21, 2015 at 01:58 PM
We use a centralized point for gameLogic so we put an .exe outside assets folder. The hierarchy is as follows:
(Assets): D:/Projects/ProjectName/Assets
(GameLogic): D/Projects/ProjectName/offline
(Database): D:/Projects/ProjectName/offline/db
Here is the code to fetch the desired .exe relative to assets
void Start()
{
StartServerProcess();
}
void StartServerProcess()
{
var stringPath = Application.dataPath + "/../offline/";
string directory = Path.GetFullPath(stringPath);
var myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.WorkingDirectory = directory;
myProcess.StartInfo.FileName = "offline.exe";
myProcess.Start();
}
Your answer
