system.IO.DirectoryNotFoundException:找不到路径的一部分

我正在为android开发unity3d应用程序,我尝试使用File.Move将上传的文件从用户目录移动到android中的Application.persistentDataPath,它在windows中工作正常,但是在我将其导出到android后,它给了我这个错误


system.IO.DirectoryNotFoundException:找不到路径的一部分


这是我正在处理的代码


FileBrowser.ShowLoadDialog( (path) => {

        var temp = path.Split('\\');

        string filename = "";

        foreach (var item in temp)

        {

            filename = item;

        }

        // Path.Combine(path);

        var dir = Application.persistentDataPath + "/upload";


        if (!Directory.Exists(dir)) 

        {

            Directory.CreateDirectory(dir); // create /Upload dir


            return;

        }else{

            Debug.Log("That path exists already.");

        }


        try{ 

            //Move the selected file

            File.Move(path,dir + '/' + filename); 

            Debug.Log(dir + '/' + filename);

            text.GetComponent<Text>().text = dir + '/' + filename;

        }catch(Exception e){

            text.GetComponent<Text>().text = e.ToString();

        }


        // FileUtil.MoveFileOrDirectory(path, Application.persistentDataPath + '/' + filename);

         }, 

                                   () => { Debug.Log( "Canceled" ); }, 

                                   false, null, "Select Folder", "Select" );


}

有人可以帮我解决这个问题吗?


繁花不似锦
浏览 615回答 2
2回答

当年话下

不要使用字符串连接+和/获取系统路径。而是始终使用Path.Combine它根据目标平台自动使用正确的路径分隔符。另外,您使用path.Split('\\');此功能的地方可能适用于路径分隔符所在的 Windows,\但可能不适用于通常使用路径分隔符的 Android,/因此最好split使用Path.DirectorySeparatorChar在循环中执行它是相当多余的,即使您出于Split某种原因想要使用它,只需执行以下操作就会更有效var temp = path.Split(Path.DirectorySeparatorChar); var filename = temp[temp.Count - 1];或者直接使用Path.GetFileName它仅返回给定路径的文件名部分。FileBrowser.ShowLoadDialog(    (path) =>     {        var temp = Path.GetFileName(path);        var dir = Path.Combine(Application.persistentDataPath, "upload");        if (!Directory.Exists(dir))         {            Directory.CreateDirectory(dir); // create /Upload dir            // WHY DO YOU RETURN HERE?           //return;        }        else        {            Debug.Log("That path exists already.");        }        try        {             //Move the selected file            var filePath = Path.Combine(dir, filename);            File.Move(path, filePath);             Debug.Log(filePath);            text.GetComponent<Text>().text = filePath;        }        catch(Exception e)        {            text.GetComponent<Text>().text = e.ToString();        }     },      () => { Debug.Log( "Canceled" ); },      false,      null,      "Select Folder",      "Select");

莫回无

有时,当将项目保存在路径名很长的驱动器中时,会发生此错误,我不记得了,但路径名有字符限制,我已将项目保存在文件夹内的文件夹中,当我从文件夹中剪切项目时从子文件夹粘贴到主文件夹中,然后路径大小减小,然后再次统一添加项目并打开,然后就没有错误了。
打开App,查看更多内容
随时随地看视频慕课网APP