- Home /
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.
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
}
}
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?
That is strange, works for me. I have edited the answer with an example script.
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
Follow this Question
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