Search Unity

Mac/Windows path

Discussion in 'Scripting' started by Futurebear, Jul 4, 2011.

  1. Futurebear

    Futurebear

    Joined:
    Jan 11, 2010
    Posts:
    97
    Currently to read or write files I am having to use
    Code (csharp):
    1.  
    2.             if (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor)
    3.             {
    4.                 saveGameFile.Load(Application.dataPath + "\\USERDATA\\" + curPlayer + ".xml");
    5.             }
    6.             if (Application.platform == RuntimePlatform.OSXPlayer)
    7.             {
    8.                 saveGameFile.Load(Application.dataPath + "/USERDATA/" + curPlayer + ".xml");
    9.             }
    10.  
    Which is getting annoying and long because I have to include this anytime I want to transverse a different directory. Is there a universal way to type application paths so I don't have to do the \\ and / ?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Path characters in .NET are just / for everything, regardless of Mac or Windows. If you wanted to use \ for Windows anyway, you could define a path char depending on the platform, and use pathChar instead of hard-coding / and \ everywhere.

    --Eric
     
  3. Futurebear

    Futurebear

    Joined:
    Jan 11, 2010
    Posts:
    97
    oh, well that saves me alot of time. I don't know where I got using \\ for windows and / for mac. Thanks!
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Actually the / part has nothing to do with .NET I think, since Windows NT4 or at latest 2000, windows has always supported / instead of \ in the paths. the \ enforcement only existed on the "insecure perma-BSOD" media OS aka Win95, 98 and ME and naturally the 3.1 and earlier versions
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I'd like to know whose bright idea it was to use a common escape character as a path character in the first place....

    --Eric
     
  6. t_w

    t_w

    Joined:
    Aug 4, 2015
    Posts:
    55
    "file:///"+"C:/Users/m/AppData/LocalLow/DefaultCompany/test_heart\myfile1.wav"

    when I use this path for www, I usually got nothing, why?
     
  7. syscrusher

    syscrusher

    Joined:
    Jul 4, 2015
    Posts:
    1,104
    As an aside, take a look at System.IO.Path.PathSeparator.
     
  8. t_w

    t_w

    Joined:
    Aug 4, 2015
    Posts:
    55
    thanks,