我試圖在網路5.0版本上使用visual studio創建一個控制臺應用程式。目的是讀取一個目錄中的所有PNG檔案,然后用代碼制作一個JSON檔案:
{
"format_version"。"1.16.100"。
"minecraft:texture_set": {
"color": "*Filename*",
"metalness_emissive_roughness": "*Filename_mer*".
}
}
在里面。是的這是為Minecraft準備的。檔案名和檔案名_mer是由代碼自動填寫的,但這不是我的問題。
static void Main(string[] args)
{
Console.WriteLine("Goto " Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) "PackagesMicrosoft。 MinecraftUWP_8wekyb3d8bbweLocalStategamescom.mojang
esource_packs" " 并找到你要生成檔案的資源包")。
string pathname = Console.ReadLine();
#region Message
Console.WriteLine()。
Console.WriteLine("正在尋找檔案夾...")。
#endregion。
string path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) "PackagesMicrosoft. MinecraftUWP_8wekyb3d8bbweLocalStategamescom.mojang
esource_packs" pathname " extures"/span>。
#region Message
Console.WriteLine()。
Console.WriteLine("Folder found in"/span>)。
Console.WriteLine(path);
#endregion。
string[] directories = Directory.GetDirectories(path)。
foreach (string paths in directories)
{
string[] files = Directory.GetFiles(paths)。
foreach (string file in files)
{
string filenameWithType = file.Substring(file.LastIndexOf("/span>) 1) 。
string filename = filenameWithType.Substring(0, filenameWithType.LastIndexOf("。"))。
string thisPath = file.Substring(0, file.LastIndexOf("/span>))。
if (filenameWithType.Substring(filenameWithType.LastIndexOf("。") 1) == "png")
{
string newPath = thisPath "/span> filename ".texture_set.json"/span>;
File.Create(newPath)。
List<string> codeInFile = new List<string>()。
codeInFile.Clear()。
codeInFile.Add("{")。
codeInFile.Add(""format_version": "1.16.100"," );
codeInFile.Add(""minecraft:texture_set": {"/span>)。
codeInFile.Add(""color": "" 檔案名 "",")。)
codeInFile.Add(""metalness_emissive_roughness": "" 檔案名 "_mer"")。)
codeInFile.Add("}")。
codeInFile.Add("}")。
TextWriter tw = new StreamWriter(newPath, false);
foreach (string line in codeInFile)
{
tw.WriteLine(line)。
Console.WriteLine(line);
}
tw.Close()。
string newPathtxt = thisPath "/span> filename "_mer.png"/span>;
Bitmap bitmap = new Bitmap(file)。
using (Bitmap b = new Bitmap(bitmap.Width, bitmap.Height))
{
using (Graphics g = Graphics.FromImage(b))
{
g.Clear(Color.Green)。
}
b.Save(newPathtxt, ImageFormat.Png)。
}
}
}
}
#region Message
Console.WriteLine("")。
Console.WriteLine("All done, Your good to go!")。
#endregion。
Console.Read()。
}
這是我所有的代碼,在檔案上。它讀取一個你輸入的目錄,查看紋理檔案,并為其中的每個PNG檔案創建一個JSON檔案,并在前面指定的代碼。盡管在運行時,代碼給了我這個錯誤。
System.IO.IOException: 'The process cannot access the file 'C:UsershttpsAppDataLocalPackagesMicrosoft.MinecraftUWP_8wekyb3d8bbweLocalStategamescom.mojang
esource_packsVanilla_Resource_Pack_1.17.10 extureslocksacia_trapdoor.texture_set.json' 因為它正在被其他行程使用。
我找不到任何符合我的問題的答案,我將感謝任何幫助。
這是一個Minecraft Bedrock版的資源包,不知道這是否有幫助。
uj5u.com熱心網友回復:
不要這樣做File.Create(newPath);或者重新考慮你的問題。它看起來像一個打字錯誤
File.Create(newPath)正在創建一個FileStream并將其丟棄,使檔案處于開放狀態并帶有share lock。如果你想預先創建檔案,至少要使用using陳述句。
using(File.Create(newPath))。
或者 關閉
File.Create(newPath).Close()。
或者
File.WriteAllText(newPath, String.Empty)。
或者
File.WriteAllBytes(newPath, Array.Empty<byte>()) 。
如果你只是想創建或覆寫檔案StreamWriter(newPath, false)就夠了
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/333678.html
標籤:
上一篇:一個gradle專案中的多個專案
