如何在 C# 中使用程序(本身就是一个 exe 文件)输出 .exe 文件?

How to output a .exe file using a program(which in itself is an exe file) in C#?

所以这一定很容易,但在网上很难找到。所有的结果都让我找到了如何从 C# 程序中制作 exe 文件的答案。

动机:- 制作一个 exe 文件,其中 运行 是任何电视剧的随机剧集。

我通过 Visual Studio 在 C# 中创建了一个新的 WPF 应用程序,它要求用户 select 一个文件夹,然后 运行 从该文件夹中的所有文件中选择一个随机视频及其子文件夹。但我不希望用户总是去 select 文件夹。我应该在代码中包含什么,以便将 exe 文件保存在用户编辑的目录 select 的某处。然后用户可以方便地 运行 该文件本身。 这是我的代码:

        FolderBrowserDialog fbd = new FolderBrowserDialog();

        DialogResult result = fbd.ShowDialog();
        string files="a"; //Initialize


        if (result == System.Windows.Forms.DialogResult.OK)
        {
            //If user presses OK then, 'files' variable is the directory selected
            files = fbd.SelectedPath; 
        }
        else if(result == System.Windows.Forms.DialogResult.Cancel)
        {
            Close();
        }

        //All Important Video Formats Included to search for
        string[] formats = { "mp4", "avi", "mkv", "flv", "wmv", "3gp" };
        string[] Allfiles = new string[0];
        for (int i=0; i<formats.Length;i++)
        {
            string[] temp = Directory.GetFiles(files, "*" + formats[i], SearchOption.AllDirectories);
            Allfiles = AddArray(Allfiles, temp);
        }

        //Starts a random Episode
        Random episode = new Random();
        int Episode = episode.Next(0, Allfiles.Length);
        Process.Start(Allfiles[Episode]);

您可以将其写入文本文件,然后在其他应用程序中读取

添加using System.IO;

写入文本文件:

// Write the string to a file.
StreamWriter file = new System.IO.StreamWriter(txt path);
file.WriteLine(lines);

file.Close();

从文本文件读取:

String line;
try 
{
//Pass the file path and file name to the StreamReader constructor
StreamReader sr = new StreamReader("C:\Sample.txt");

//Read the first line of text
line = sr.ReadLine();

//close the file
sr.Close();
}
catch(Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
   finally 
{
Console.WriteLine("Executing finally block.");
}

如果您想关闭应用程序,只需键入 this.Close()