c# File.WriteAllBytes 不起作用/文件路径不起作用

c# File.WriteAllBytes doesn't work / Filepath doesn't work

我想下载一个程序集并将其保存到 Unity 游戏的托管文件夹中。

File.WriteAllBytes("file:///" + Application.dataPath + "/Managed/Assembly-CSharp.dll", assembly.bytes);

但是不知怎么的,它没有下载.. 它只在我使用时下载 ("Assembly-CSharp.dll");

File.WriteAllBytes("Assembly-CSharp.dll", assembly.bytes);

需要快速帮助!

您的语法有误。修正你的文件路径:

File.WriteAllBytes("file:///"...

您可以提供文件的完整路径,例如

File.WriteAllBytes("C:/Users/myFile.txt", assembly.bytes);

或者从 Unity 的主文件夹导航到文件,就像您在第二个代码中所做的那样。

可能的替代解决方案,如果你在 Mac/iOS:

If it was on iOS on the Mac, it could be defaulted to WebPlayer on the transition as you can't set an iOS build target on PC.

Solution: Set your target to one of Standalone PC when on PC.

source

只需两分钱,Pitor 的答案是正确答案。

但作为更好的做法,您应该使用 Path.Combine

你的情况:

var fileName = "/Managed/Assembly-CSharp.dll";
var filePath = Path.Combine(Application.dataPath, fileName);
File.WriteAllBytes(filePath, assembly.bytes);

而且您不必处理依赖于平台的代码。