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
0
Question by toddisarockstar · Mar 19, 2016 at 05:51 PM · directory

Copy an entire directory to another folder through script

Im building for standalone. Im trying to copy a folder in my assetts to another folder in the c drive.

when i use Directory.Move(path,path) i get an error like this:

UnauthorizedAccessException: Access to the path is denied. System.IO.Directory.Move (System.String sourceDirName, System.String destDirName) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO/Directory.cs:403) installer.Update () (at Assets/installer.js:35)

ive also tryed FileUtil.CopyFileOrDirectory ("path", "path")

it works just fine in the editor but when building, it produces a compiler error on that line only when i build saying FileUtil is an unknown identifier.

i am very fimiliar with creating individual directorys and files. and im sure i could manually write a code too loop through all of them and write them one at a time but i was hopeing someone had an easier answer to my problem or any ideas what i might be doing wrong in my first two attempts.

Thanks!

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
1

Answer by RobAnthem · Dec 09, 2016 at 06:19 AM

Best way I know of is this. I've ran into lots of copy permission issues in the past and usually it comes down to the destination folders permission access. You probably already tried this, but if System.IO can't do it, Unity does have its own Serializing methods, but you'd again... have to iterate the files Directly from MSDN How to: Copy, Delete, and Move Files and Folders (C# Programming Guide)

     // Simple synchronous file copy operations with no user interface.
     // To run this sample, first create the following directories and files:
     // C:\Users\Public\TestFolder
     // C:\Users\Public\TestFolder\test.txt
     // C:\Users\Public\TestFolder\SubDir\test.txt
     public class SimpleFileCopy
     {
         static void Main()
         {
             string fileName = "test.txt";
             string sourcePath = @"C:\Users\Public\TestFolder";
             string targetPath =  @"C:\Users\Public\TestFolder\SubDir";
 
             // Use Path class to manipulate file and directory paths.
             string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
             string destFile = System.IO.Path.Combine(targetPath, fileName);
 
             // To copy a folder's contents to a new location:
             // Create a new target folder, if necessary.
             if (!System.IO.Directory.Exists(targetPath))
             {
                 System.IO.Directory.CreateDirectory(targetPath);
             }
 
             // To copy a file to another location and 
             // overwrite the destination file if it already exists.
             System.IO.File.Copy(sourceFile, destFile, true);
             
             // To copy all the files in one directory to another directory.
             // Get the files in the source folder. (To recursively iterate through
             // all subfolders under the current directory, see
             // "How to: Iterate Through a Directory Tree.")
             // Note: Check for target path was performed previously
             //       in this code example.
             if (System.IO.Directory.Exists(sourcePath))
             {
                 string[] files = System.IO.Directory.GetFiles(sourcePath);
 
                 // Copy the files and overwrite destination files if they already exist.
                 foreach (string s in files)
                 {
                     // Use static Path methods to extract only the file name from the path.
                     fileName = System.IO.Path.GetFileName(s);
                     destFile = System.IO.Path.Combine(targetPath, fileName);
                     System.IO.File.Copy(s, destFile, true);
                 }
             }
             else
             {
                 Console.WriteLine("Source path does not exist!");
             }
 
             // Keep console window open in debug mode.
             Console.WriteLine("Press any key to exit.");
             Console.ReadKey();
         }
     }


Comment
Add comment · Show 1 · 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 jesusluvsyooh · Apr 13, 2018 at 02:36 PM 0
Share

This still works today, thank you!

avatar image
0

Answer by ForceMagic · Dec 08, 2016 at 09:01 PM

I also had unconsistent results with Directory.Move, got rid of it and used a classic Copy/Delete instead. I put the copy code on SO.

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

41 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

Related Questions

Saving File in new directory - Unauthorized Access Exception 1 Answer

How do I load a config (.cfg) or ini file? 2 Answers

How to get the next file in a directory? 1 Answer

EditorUtility.OpenFilePanel uses \ FileInfo.Name uses / 0 Answers

Getting Directory not Found error with Binary Formatting attempt 2 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