Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to use System.IO when dealing with files in Unity3d?

I am writing a game that needs to access a file. That is, it has to write to a file and read from it. The question is whether I can use the System.IO classes without having to worry about problems that might occur due to platform incompatibility.

Do these methods work regardless of the platform that the game is running on? If not, is it possible to work with files independently from the platform?

like image 524
HerpDerpington Avatar asked Oct 27 '25 03:10

HerpDerpington


1 Answers

Yes they work regardless of where you run it. Telling from my own experience, and have no idea why this could be otherwise.

Want to clarify this a bit. Of course you should use common sense when working with paths on different platforms. Usually you only work with relative paths in code, and when you work with absolute paths, you usually get them from your configuration or from another code (example: Path.GetTempPath()). Unless you hardcode your absolute paths and combine relative paths by hand (i.e. "mydir/myanotherdir") you should be fine. And even if you don't, mono has portability layer (read here - http://www.mono-project.com/docs/advanced/iomap/) to allow easy porting IO code from windows to linux. However don't rely on it and:

  1. Don't hardcode absolute paths. Get them from some configuration file different for each platform (if really necessary), or from calling built-in methods (i.e. Path.GetTempPath, AppDomain.CurrentDomain.BaseDirectory) and so on.
  2. Always use provided methods to combine paths (Path.Combine, Path.DirectorySeparatorChar and so on). Then you will be fine.
like image 53
Evk Avatar answered Oct 28 '25 22:10

Evk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!