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

时间:2017-07-10 14:27:08

标签: c# wpf visual-studio exe

所以这一定很容易但很难在网上搜索。所有结果都为我提供了如何用C#程序制作exe文件的答案。

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

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

        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]);

1 个答案:

答案 0 :(得分:0)

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

添加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()

即可