代码之家  ›  专栏  ›  技术社区  ›  Spell Cin

是否通过项目内部的代码更改与项目位于同一文件夹中的文本文档的内容?

  •  0
  • Spell Cin  · 技术社区  · 2 年前

    是否可以通过项目内部的代码更改与项目位于同一文件夹中的例如文本文档的内容? 使用Visual Studio社区2022 使用模板:Windows窗体应用程序(.NET Framework)

    我试着搜索这个问题,但什么也找不到。

    1 回复  |  直到 2 年前
        1
  •  0
  •   navid abbasnejad    2 年前

    您可以在Visual Studio项目中编写以下内容:

    using System;
    using System.IO;
    
    namespace ConsoleApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                ModifyTextDocument("File.txt", "New Text");
            }
    
            public static void ModifyTextDocument(string fileName, string newText)
            {
                // Get the full path of the text document in the same folder as your project
                string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
    
                try
                {
                    // Check if the file exists
                    if (File.Exists(filePath))
                    {
                        // Write the new text to the text document, overwriting its previous content
                        File.WriteAllText(filePath, newText);
                    }
                    else
                    {
                        // Handle the case where the file does not exist
                        // For example, you could create a new file with the given name and content
                        File.WriteAllText(filePath, newText);
                    }
                }
                catch (Exception ex)
                {
                    // Handle any exceptions that may occur during file manipulation
                    Console.WriteLine($"An error occurred: {ex.Message}");
                }
            }
        }
    }
    

    此代码将写入您的项目/bin/debug文件夹,发布后将写入可执行文件所在的主文件夹。但是,如果要更改文件夹,请执行以下操作:

    以下是如何在Visual Studio中更改工作目录:

    1.在解决方案资源管理器中右键单击您的项目。

    2.从上下文菜单中选择“属性”。

    3.在项目属性窗口中,转到“调试”选项卡。

    4.在“工作目录”字段中,设置要创建文件的目录的路径。您可以使用宏$(ProjectDir)来引用项目目录。

    或者,您可以使用目录。GetParent方法从bin/debug目录向上导航一个目录以到达项目目录。以下是如何修改代码以实现此目的:

    using System;
    using System.IO;
    
    namespace ConsoleApp
    {
        public class FileManipulator
        {
            public void ModifyTextDocument(string fileName, string newText)
            {
                // Get the parent directory of the current working directory
                string projectDirectory = Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).Parent.FullName;
    
                // Combine the project directory with the file name
                string filePath = Path.Combine(projectDirectory, fileName);
    
                try
                {
                    // Write the new text to the text document, overwriting its previous content
                    File.WriteAllText(filePath, newText);
                }
                catch (Exception ex)
                {
                    // Handle any exceptions that may occur during file manipulation
                    Console.WriteLine($"An error occurred: {ex.Message}");
                }
            }
        }
    }
    

    如果您需要更多帮助,请提供更多详细信息。

    我在控制台应用程序中编写此代码,您也可以在Windows窗体应用程序中使用此代码。只需确保包含必要的命名空间(System和System.IO),并在Windows窗体应用程序中适当地调用ModifyTextDocument方法