使用项目中添加的模板文件在运行C#时创建文件

使用项目中添加的模板文件在运行C#时创建文件,c#,.net,C#,.net,在我的C#项目中添加了一个文本文件,它包含静态值(大约300行)。是否可以使用项目中附加的模板文件内容在指定路径和文件名中创建txt。当二进制文件运行时,它必须在指定路径中创建一个包含模板文件中可用内容的txt 最好的解决方案可能是将文件添加为项目资源。右键单击C#项目并选择属性,然后选择资源。如果出现提示,请单击链接添加资源文件,然后选择将文件添加为项目资源的选项。然后,您可以通过[Project Namespace].Properties.[file Name]访问C代码中的文件内容。最好的

在我的C#项目中添加了一个文本文件,它包含静态值(大约300行)。是否可以使用项目中附加的模板文件内容在指定路径和文件名中创建txt。当二进制文件运行时,它必须在指定路径中创建一个包含模板文件中可用内容的txt


最好的解决方案可能是将文件添加为项目资源。右键单击C#项目并选择属性,然后选择资源。如果出现提示,请单击链接添加资源文件,然后选择将文件添加为项目资源的选项。然后,您可以通过
[Project Namespace].Properties.[file Name]
访问C代码中的文件内容。最好的解决方案可能是将文件添加为项目资源。右键单击C#项目并选择属性,然后选择资源。如果出现提示,请单击链接添加资源文件,然后选择将文件添加为项目资源的选项。然后,您可以通过
[Project Namespace].Properties.[file Name]
访问C代码中的文件内容。有两个步骤:

1)将模板文件作为嵌入式资源添加到项目中

在解决方案资源管理器中单击
qaxxxvbsfile.txt
,并将
构建操作设置为
嵌入式资源

2)使用
GetManifestResourceStream()
读取此资源,并在新文本文件中写入数据

using System;
using System.IO;
using System.Reflection;

namespace AutomateDigicall
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get current assembly
            var assembly = Assembly.GetExecutingAssembly();
            var resourceName = "AutomateDigicall.QAXXXXVBSFile.txt";

            string template = String.Empty;

            // Read resource from assembly
            using (Stream stream = assembly.GetManifestResourceStream(resourceName))
            using (StreamReader reader = new StreamReader(stream))
            {
                template = reader.ReadToEnd();
            }

            // Write data in the new text file
            var filePath = "C:\\TestFolder\\myFileName.txt";
            using (TextWriter writer = File.CreateText(filePath))
            {
                writer.WriteLine("Text from template below:");
                writer.WriteLine(template);
            }
        }
    }
}
有两个步骤:

1)将模板文件作为嵌入式资源添加到项目中

在解决方案资源管理器中单击
qaxxxvbsfile.txt
,并将
构建操作设置为
嵌入式资源

2)使用
GetManifestResourceStream()
读取此资源,并在新文本文件中写入数据

using System;
using System.IO;
using System.Reflection;

namespace AutomateDigicall
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get current assembly
            var assembly = Assembly.GetExecutingAssembly();
            var resourceName = "AutomateDigicall.QAXXXXVBSFile.txt";

            string template = String.Empty;

            // Read resource from assembly
            using (Stream stream = assembly.GetManifestResourceStream(resourceName))
            using (StreamReader reader = new StreamReader(stream))
            {
                template = reader.ReadToEnd();
            }

            // Write data in the new text file
            var filePath = "C:\\TestFolder\\myFileName.txt";
            using (TextWriter writer = File.CreateText(filePath))
            {
                writer.WriteLine("Text from template below:");
                writer.WriteLine(template);
            }
        }
    }
}

是的,你可以。。发布一些您试图获得更好解决方案的代码参考:是的,您可以。。发布一些您试图获得更好解决方案的代码参考:谢谢您的回复。谢谢您的回复。谢谢您的回复。谢谢您的回复。