File read/write paradigms on Unity- which to use? - c#

I want to load some JSON files that are stored in my assets.
I don't know where I'm supposed to store them in the assets. Should I store it in the "Resources" folder, in the "StreamingAssets" folder or somewhere else ?
I don't know what path I'm supposed to use. I heard you need to write : path = "jar:file://" + Application.dataPath + "!/assets/"; for the StreamingAssets but there is also Application.streamingAssetsPath. I also heard you always need to use Application.persistantDataPath. So which path should I use ?
I don't know what I can use in to access the files in C#. I've heard you can use Directory.Exists() and File.Exists() but if you use the StreamingAssets you must use WWW. Is that right?

You can get a basic idea in this answer.
In addition, I would suggest placing anything you want to package with your game in your Resources folder, which you can access by using Resources.Load(). You can iterate through these directories however you want- ex: Resources.Load("folder/file");.
If you want to write files, they have to go in Application.persistentDataPath as that's the only write-able directory (that's specified by Unity), i.e. for run-time files only. But once a file is stored there, it is persistent between sessions.
You can, of course, specify absolute paths and try to load assets from there, but that's more risk than necessary (paths differ on different devices).
You could ignore Application.dataPath altogether, until you find a specific need for it (ex: to access files that are not in persistentDataPath, but not in resources either).
I hope that helps!

Related

Tracking the location of a file in C# windows?

I need to track the location of some files that i know the initial full paths. Situtation as follows :
I have a file in path C:\Temp\first.txt
in some time this first.txt file changes location to for example C:\Temptwo folder
i need to learn this second location automatically when the location change happens is there way to do it in C# thanks.
It depends on where the files are moving from and to. If you are moving from C:\\MyProgram\TempA\Temp.txt to C:\\MyProgram\TempB\Temp.txt, then you can just use the directory tools. Directory.GetFiles will scan a directory for you and return an array of strings containing the names of those files. If you know the name of the file you are looking for, or you are using a unique file extension, then you can search through the files you find until you identify the file you are looking for. You can also call Directory.GetDirectories recursively to look through sub-directories if you have a lot of folders this file could be hiding in.
While this might technically work on the C drive, I would suggest carefully considering your use case before trying to re-invent Windows Search.

File path for c# when moved to a memory stick

sorry first time user and currently learning c# at uni, i'm working on an assignment and i'm trying to get the file path to work on the memory stick as that's what i need to hand it in on, thanks many regards
I'm not completely sure what your question is by I think what you're talking about is absolute vs relative paths. If you use an absolute path like "C:\users\yourname\blalba\project\stuff", then it's obviously only going to only work on your computer. However, you mostly all of the time want to use relative paths. Relative paths have the root directory of the build output files for your project; where your .exe file is built for your project. This is usually in "projectdir\bin\debug" or "projectdir\bin\release". So if you put for example a file called 'test.txt' in that directory, you can simply put the relative path "test.txt" instead of "C:\users\yourname\blalba\project\bin\debug\test.txt". If you were to put 'test.txt' in the project directory, you can use the relative path "....\test.txt". "..\" means navigating one step back.
The path to the directory containing the files you are looking for is "F:\Mod005244, 1715840". The "1715840 JH" is just the name of the drive. You access different drives via the drive letter. In this case, the drive letter "F" was assigned to your flash drive.
I would suggest making the file path configurable or request input from the user of the program as the drive letter of a flash drive will not always be "F". There are even classes (such as FileBrowserDialog) that will open a graphical file browser dialog and prompt the user to navigate and select a file.

Add Multiple Folders / Directories to a ZipFile

I am writing a backup program that requires predefined multiple folder(s) & single file(s) to be added to a single zip archive. I have had no issues adding a single folder using -ZipFile.CreateFromDirectory(string, string, c..level, bool(false))
However i am having a hard time adding multiple folders as there does not seem to be a way to update an archive or target two folders using the CreateFromDirectory method.
- Would be nice if there was an UpdateFromDirectory mehod!
I have been trying to stay away from third party libraries for no reason really, however as far as i have found none deal with multiple unrecursive folders.
I have tried just about everything other than writing my own code to recurse & add individually which i don't really want to do.
The program has several inputs that defines the folders / files to be zipped and depending on whether they are not null should add them to a single zip file regardless of whether they are a folder or file.
I guess my question is whether this is possible at all using the boxed libraries without custom recursing or even with a third party library without heavy mods... Not sure if i have made my question clear, sure you will all let me know if i have not.
From what I can tell using the ZipFile class you can only create and read. if you want to update you would need to create the whole zip again. [Source: ZipFile methods]
to target more than one folder you could arrange all the files and folders into one folder then zip the entire source without including the source folder. In most cases moving this files/folders isn't possible so I'd recommend looking into Symlinks within windows. I'd redirect to you [Issue with creating symbolic link to directory
You can create a "myFolder" folder and put in it all the folders you want found in the zipped folder. Then do ZipFile.CreateFromDirectory("myFolder", "name of zip file to create", CompressionLevel.Fastest, false, Encoding.UTF8). Overriding this IncludeBaseDirectory method to false allows this to be done.

How to make file path from stream for class or method that need to use it

I am looking for idea or hint how fill method/class that need file path in parameter.
f.g
InserFileIntoLibrary(string filePath);
where file path is not C:/ and any other buffored temp directory. Is it possible to make some symulation of file directiory and insert it's file.path value into that string? Problem to solve is that i am sending file between servers, and on other server i have method that can only use memory from program and NO DIRECTORY to buffor/temp this file/stream. Someone has this kind of problem? Or know where to look?
p.s sorry for my english, any kind of corrects are welcom.
Basically no - if you can't save file to disk than you can't get path to such file.
In theory you may be able to create (or use existing) virtual file system, but depending on your budget it may not be acceptable solution. One built-in version is to use WebDAV to access server as \\host\file...
I'd recommend first try to save streams locally - temp folder is normally writable and getting permissions to do so may be way easier.
Also make sure there is no API that performs same task but accepts streams.

Best practice for building file paths in C#

I'm working on a C# project where I must build paths to various files and folders. These are all under one root folder which I have specified in my Web.config file.
For example:
"start with: "D:\builds\" from my Web.config
Pass to GetRelativePath() to get "D:\builds\5.2\5.2.9751"
Then pass to GetAutoSuitePath() to get "D:\builds\5.2\5.2.9751\AutoSuite\"
Then pass to ParseBrLog which will read "D:\builds\5.2\5.2.9751\AutoSuite\AASanity.csv"
My paths are correct, but I just want to know what the best practice is for incomplete paths. Should I add a "\" to the end of every folder ("D:\Builds\5.2\" + "test.txt"), or add one to the start of every folder/file I add to the path ("D:\Builds" + "\5.2" + "\test.txt")? Currently, I'm, doing it both ways, and want to choose one uniform way of doing it.
Use the Path class to build up your paths. It will do the right thing.
Performs operations on String instances that contain file or directory path information. These operations are performed in a cross-platform manner.
var full = Path.Combine(baseDir, dirFragment);
Use Path.Combine to concatenate path tokens.
If the path is a file to set/change the extension use Path.ChangeExtension.

Categories