Search code examples
c#windows-forms-designer

Reading from file in Release version of c# app


I have a problem with my app in C#.

My program is supposed to read from the Resources file named TextFile1.txt paths to some files and parse them as arguments to another executable script.

In debug version program works well and after changes in TextFile1 program takes updated arguments but in Release version app does not update this changes.

Here is C# function that reads from file

private void button2_Click(object sender, EventArgs e)
{
    string selectedItem = (string)listBox1.SelectedItem;
    if (selectedItem != null)
    {
        string[] parts = selectedItem.Split('\\');
        string NX_exe = "\\App.exe";
        string path = string.Join("\\", parts, 0, parts.Length - 1);
        string arguments = string.Join(" ", destinationPath, path + NX_exe);

        byte[] fileBytes = Properties.Resources.SignDotNet_Script;

        string exeFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Script.exe");
        File.WriteAllBytes(exeFilePath, fileBytes);

        ProcessStartInfo startInfo = new ProcessStartInfo();

        startInfo.FileName = exeFilePath;

        if (destinationPath == "")
        {
            MessageBox.Show("No Destination Path", "SignDotNetApp",
            MessageBoxButtons.OK, MessageBoxIcon.Warning);
            return;
        }

        if (checkBox1.Checked)
        {
            startInfo.Arguments = destinationPath + " " + '"' + path + NX_exe + '"' + " y";
        }
        else
        {
            startInfo.Arguments = destinationPath + " " + '"' + path + NX_exe + '"';
        }

        Process process = Process.Start(startInfo);

        process.WaitForExit();

        MessageBox.Show("Signing Completed", "SignDotNetApp",
            MessageBoxButtons.OK, MessageBoxIcon.Information);

        File.Delete(exeFilePath);
    }
}

and here is an example of TextFile1

ProgramName1,   C:\Program Files\ExecutablePath_1\App.exe
ProgramName2,   C:\Program Files\ExecutablePath_2\App.exe
ProgramName3,   C:\Program Files\ExecutablePath_3\App.exe

I do not know how to change that and I would appreciate some advice about this issue


Solution

  • Okey I found the answer to that question, it's not the most brilliant but it works for me.

    string pathToMyFile = "Resources\\Paths.txt";
    
                string pathFile = Path.Combine(Directory.GetCurrentDirectory(), pathToMyFile);
    
    
                using (StreamReader reader = new StreamReader(pathFile))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                         some code
                    }
                }
    

    Previously I was using

    string resourceName = "SignDotNetApp.Resources.TextFile1.txt";
                
    Assembly asm = Assembly.GetExecutingAssembly();
    
    using (StreamReader reader = new StreamReader(asm.GetManifestResourceStream(resourceName)))
                {
                    some code
                }
    
    

    and my program, by this instruction, was storing Paths.txt at first build

    Now at each run, it's reloading Paths.txt and applying changes