Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ochreous · Dec 28, 2012 at 07:27 PM · c#filexml

C# Getting only the Name of the XML File and not the Directory of the XML File

Hi everyone, is there a way to get only the name of an xml file and not the directory of the file? I declared public string[] XMLfiles = Directory.GetFiles(Application.dataPath,"*.XML"); which does list the XML files but it doesn't get the names of the files . In the elements it lists where the XML files are located within the project but I want the names only. Is there a way to do that?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by stfx · Dec 28, 2012 at 08:10 PM

When you have a string of the full path (which you get from Directory.GetFiles) you can use the .NET method System.IO.Path.GetFileName(fullPath) to get the file name plus extension.

 // GetFileName('C:\mydir\myfile.ext') returns 'myfile.ext'
 // GetFileName('C:\mydir\') returns ''


Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ochreous · Dec 28, 2012 at 08:46 PM 0
Share

Is there a way to get the file name without referencing it directly? Like with for loops. Example

public string[] X$$anonymous$$Lfiles = Directory.GetFiles(Application.dataPath,"*.X$$anonymous$$L");

public string ExampleString;

void OnGUI(){

for(int a = 0; a < X$$anonymous$$Lfiles.Length; ++a){

if(GUILayout.Button("ExampleButton")){

ExampleString = X$$anonymous$$Lfiles[a];

 }

}

}

What exactly would I need to change in the above code to do that?

avatar image stfx · Dec 28, 2012 at 08:51 PM 0
Share

Of course:

ExampleString = System.IO.Path.GetFileName(X$$anonymous$$Lfiles[a]);

avatar image liszto · Dec 28, 2012 at 10:29 PM 2
Share

And if you don't want he extension and only your file name you can use this : http://msdn.microsoft.com/en-us/library/system.io.path.getfilenamewithoutextension.aspx

the function : GetFileNameWithoutExtension( string path )

avatar image
0

Answer by Geo.Ego · Dec 28, 2012 at 10:10 PM

Use System.IO.Path.GetFileName after getting your string array.

 public string[] XMLfiles = Directory.GetFiles(Application.dataPath,"*.XML");
 foreach(string file in XMLfiles)
 {
     print(Path.GetFileName(file));
 }

Of course, you'll need to modify that to do what you want with each file name.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

JSON vs XML for Unity C# 1 Answer

Best practice to store and load a specific set of objects? 3 Answers

Creating a save.xml file and a folder to stash it in 1 Answer

How to find the name of an xml I am opening 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges