-2

我正在尝试使用 Visual Studio 在网络版本 5.0 上创建一个控制台应用程序。目的是读取一个目录下的所有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) + "\\Packages\\Microsoft.MinecraftUWP_8wekyb3d8bbwe\\LocalState\\games\\com.mojang\\resource_packs" + " And find the resource pack you want to generate files into");
        string pathname = Console.ReadLine();
        #region Message
        Console.WriteLine();
        Console.WriteLine("Looking for folder...");
        #endregion
        string path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Packages\\Microsoft.MinecraftUWP_8wekyb3d8bbwe\\LocalState\\games\\com.mojang\\resource_packs\\" + pathname + "\\textures";
        #region Message
        Console.WriteLine();
        Console.WriteLine("Folder found in");
        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("\\") + 1);
                string filename = filenameWithType.Substring(0, filenameWithType.LastIndexOf("."));
                string thisPath = file.Substring(0, file.LastIndexOf("\\"));
                if (filenameWithType.Substring(filenameWithType.LastIndexOf(".") + 1) == "png")
                {
                    string newPath = thisPath + "\\" + filename + ".texture_set.json";
                    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\": {");
                    codeInFile.Add("\"color\": \"" + filename + "\",");
                    codeInFile.Add("\"metalness_emissive_roughness\": \"" + filename + "_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 + "\\" + filename + "_mer.png";
                    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: '进程无法访问文件'C:\Users\https\AppData\Local\Packages\Microsoft.MinecraftUWP_8wekyb3d8bbwe\LocalState\games\com.mojang\resource_packs\Vanilla_Resource_Pack_1.17.10\textures\blocks\acacia_trapdoor .texture_set.json' 因为它正被另一个进程使用。

我找不到任何适合我的问题的答案,我将不胜感激。

顺便说一句,这是一个 Minecraft 基岩版资源包,不确定是否有帮助。

4

1 回答 1

1

不要这样做File.Create(newPath);或重新考虑你的问题。它看起来像一个错字。

简而言之File.Create(newPath),就是创建一个FileStream并丢弃它,让文件保持打开状态并带有一个共享锁。如果您尝试预先创建文件,请至少使用以下using语句:

using (File.Create(newPath));

或者Close

File.Create(newPath).Close();

或者

File.WriteAllText(newPath, String.Empty);

或者

File.WriteAllBytes(newPath, Array.Empty<byte>());

如果您只是尝试创建或覆盖文件StreamWriter(newPath, false)就足够了。

于 2021-09-22T08:46:50.477 回答