How to copy unity.3d file to Application.dataPath directory using WriteAllBytes

Hello i have been able to get a hold of my unity.3d asstet bundle files using File Browser Dialogue in my Unity GUI.

I currently use this code to attempt read all bytes from the selected file and write them to a directory

	string filePath = ("my file path");
			
			string destinationPath = "D:/projects/New folder";

byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);
				System.IO.File.WriteAllBytes(destinationPath,fileBytes);

but i get this error.

UnauthorizedAccessException: Access to the path 'D:/projects/New folder' is denied.

New to files in unity so I won't post this as an answer, but... are you sure you have permission for this folder? Try running the unity standalone as admin.

–

I thought of doing that but I have only run it from inside the game engine

–

Umm... Try running Unity as admin? Also, what OS are you using?

–

I'm using Windows but i'm building my game to run on Linux and mac also . to run as admin is a bit too rough around the edges. so i was wondering if there was a way to write to the directory without having to run as admin.

–

The only way to do it without running as admin is to set the correct permissions for the folder.

–

1 Answer

1

You most likely don’t have permission to write to this folder. You can try either running Unity or your executable as administrator, as mentioned by dvidunis, or change the folders permissions. If you’re on Windows, which I am guessing you are because of the file path, you can change the permissions as shown here: http://answers.microsoft.com/en-us/windows/forum/windows_vista-files/how-do-i-change-folder-and-file-permissions/465f2b42-63dd-4486-8dd1-c870290efeed
If you are on OSX or linux, open up your terminal and type

chmod +rw (path to your folder)

thanks . i am trying to achieve accessibility to folders without having to manually change permissions.

–

This should help you: using System.Diagnostics; #if UNITY_STANDALONE_WIN Process.Start("cacls yourfolder /g everyone:f"); #endif #if UNITY_EDITOR //add the code for the platform you are developing on #endif #else Process.Start("chmod +rw yourfolder"); #endif

–

tanks a lot for the help cmpgk1024 . since the file i'm trying to import is a unity.ed file i will just try to eve it to resources after importing it as an asset . then i will load from resources

–