亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

C#
繁花不似錦 2023-06-25 14:33:21
我正在為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" );}有人可以幫我解決這個問題嗎?
查看完整描述

2 回答

?
當年話下

TA貢獻1890條經驗 獲得超9個贊

不要使用字符串連接+/獲取系統路徑。

而是始終使用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"

);


查看完整回答
反對 回復 2023-06-25
?
莫回無

TA貢獻1865條經驗 獲得超7個贊

有時,當將項目保存在路徑名很長的驅動器中時,會發生此錯誤,我不記得了,但路徑名有字符限制,我已將項目保存在文件夾內的文件夾中,當我從文件夾中剪切項目時從子文件夾粘貼到主文件夾中,然后路徑大小減小,然后再次統一添加項目并打開,然后就沒有錯誤了。



查看完整回答
反對 回復 2023-06-25
  • 2 回答
  • 0 關注
  • 711 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號