Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by hilmimarzuqi · Apr 23, 2016 at 07:38 PM · applicationdatapath

How to delete a file using Application.dataPath ?

I have the following code in the Update() function:

         if(Input.GetKeyDown(KeyCode.F9))
         {
             File.Delete (Application.dataPath + "/Ob4.txt");
         }

I want to be able to delete the saved file whenever I press a button. But whenever I press F9 the file still remains in the Assets folder.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
6

Answer by AlucardJay · Apr 24, 2016 at 02:31 AM

You need to tab out of Unity then tab back in for the project folder to refresh. Or refresh the asset database in code:

 // refresh editor view
 #if UNITY_EDITOR
 UnityEditor.AssetDatabase.Refresh();
 #endif

http://docs.unity3d.com/ScriptReference/AssetDatabase.Refresh.html

Also, simply just check the file location with your file explorer to see if the file has been added/removed.


Edit : here is an example script. Create a new scene, attach script to an empty gameObject, and hit play. Editor project window refreshes when a file is created or deleted.

CreateAndDeleteFile.cs

 using UnityEngine;
 using System.Collections;
 using System.IO;
 
 public class CreateAndDeleteFile : MonoBehaviour 
 {
     public string fileName = "ob04";
     public string fileExtension = ".txt";
     
     private string guiMessage = "type in a file name and hit save";
     
     
     void OnGUI() 
     {
         GUI.Box( new Rect( 160, 20, 400, 25 ), guiMessage );
         
         fileName = GUI.TextField( new Rect( 20, 20, 120, 25 ), fileName );
         
         if ( GUI.Button( new Rect ( 20, 60, 100, 30 ), "LOAD" ) )
         {
             LoadFile();
         }
         
         if ( GUI.Button( new Rect ( 20, 100, 100, 30 ), "SAVE" ) )
         {
             SaveFile();
         }
         
         if ( GUI.Button( new Rect ( 20, 140, 100, 30 ), "DELETE" ) )
         {
             DeleteFile();
         }
     }
     
     
     void LoadFile() 
     {
         string filePath = Application.dataPath + "/" + fileName + fileExtension;
         
         // check if file exists
         if ( !File.Exists( filePath ) )
         {
             guiMessage = "no " + fileName + " file exists"; //Debug.Log( "no " + fileName + " file exists" );
         }
         else
         {
             guiMessage = fileName + " file exists, reading..."; //Debug.Log( fileName + " file exists, reading ..." );
             
             // read file
             StreamReader readFile = new StreamReader( filePath );
             string readString = readFile.ReadToEnd(); //Debug.Log( readFile.ReadToEnd() );
             readFile.Close();
             
             guiMessage = guiMessage + " : " + readString;
         }
     }
     
     
     void SaveFile() 
     {
         string filePath = Application.dataPath + "/" + fileName + fileExtension;
         
         // check if file exists
         if ( !File.Exists( filePath ) )
         {
             guiMessage = "no " + fileName + " file exists, creating..."; //Debug.Log( "no " + fileName + " file exists, creating..." );
             
             // create new file
             StreamWriter newFile = new StreamWriter( filePath );
             newFile.WriteLine( "Hello, I am a text file :)" );
             newFile.Close();
             
             RefreshEditorProjectWindow();
         }
         else
         {
             guiMessage = fileName + " file already exists"; //Debug.Log( fileName + " file already exists" );
         }
     }
     
     
     void DeleteFile() 
     {
         string filePath = Application.dataPath + "/" + fileName + fileExtension;
         
         // check if file exists
         if ( !File.Exists( filePath ) )
         {
             guiMessage = "no " + fileName + " file exists"; //Debug.Log( "no " + fileName + " file exists" );
         }
         else
         {
             guiMessage = fileName + " file exists, deleting..."; //Debug.Log( fileName + " file exists, deleting..." );
             
             File.Delete( filePath );
             
             RefreshEditorProjectWindow();
         }
     }
     
     
     void RefreshEditorProjectWindow() 
     {
         #if UNITY_EDITOR
         UnityEditor.AssetDatabase.Refresh();
         #endif
     }
 }

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 hilmimarzuqi · Apr 24, 2016 at 03:49 AM 0
Share

I still didn't manage to remove it though, an alternative I would go for is to let it create a new path each time I press a button, is that ok?

avatar image AlucardJay hilmimarzuqi · Apr 25, 2016 at 02:08 PM 0
Share

That is strange, works for me. I have edited the answer with an example script.

avatar image mayankela21 AlucardJay · Sep 04, 2019 at 01:59 PM 0
Share

I want error UnauthorizedAccessException: Access to the path 'D:\unity 2d\Project\SpaceGameChandrayan2\Assets\Ads' is denied. System.IO.FileStream..ctor (System.String path, System.IO.File$$anonymous$$ode mode, System.IO.FileAccess access, System.IO.FileShare

what i do!

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

43 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Application.dataPath + "/Raw" not loading texture on iOS? 0 Answers

how to access to another directory through Application.dataPath 1 Answer

Difference between Application.persistantDataPath Vs Application.dataPath 2 Answers

Path for Assets/Textures folder File in Android 2 Answers

Can I get a folder up hierarchy relative to the dataPath? 6 Answers


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