TextWriter 不工作,因为它正被另一个进程使用。网络5.0

TextWriter not working because its being used by another process. Network 5.0

我正在尝试使用 visual studio 在网络版本 5.0 上创建控制台应用程序。目的是读取一个目录下的所有PNG文件,并制作一个JSON文件,代码为:

{
    "format_version": "1.16.100",
    "minecraft:texture_set": {
        "color": "*Filename*",
        "metalness_emissive_roughness": "*Filename_mer*"
    }
}

在里面。是的,这是针对 Minecraft 的。文件名和 filename_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: 'The process cannot access the file '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' because it is being used by another process.'

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

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

不要这样做 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) 就足够了。