ScottishTapWater
ScottishTapWater

Reputation: 4846

Generating new file path not working

I'm attempting to take a file .fdf, convert it to another format .g using an external command. From there, I am trying to move the source FDF to a backup folder and the g file into a temporary folder.

To do this I am using this section of code:

private void populateTable(string[] paths)
    {
        string fdf_g = System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "Local\\FindReplace\\Bin\\fdf_g.exe";
        string g_fdf = System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "Local\\FindReplace\\Bin\\g_fdf.exe";
        System.Collections.Generic.List < string > gFiles = new System.Collections.Generic.List<string>();
        foreach (string file in paths)
        {
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.FileName = fdf_g;
            startInfo.Arguments = file;
            System.Diagnostics.Process.Start(startInfo);
            System.IO.File.Copy(file, System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "Local\\FindReplace\\BackupFDF\\" + System.IO.Path.GetFileName(file));
            System.IO.File.Move("FOX_" + System.IO.Path.GetFileName(file).Replace(".fdf",".g"), System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "Local\\FindReplace\\Temp\\" + "FOX_" + System.IO.Path.GetFileName(file).Replace(".fdf", ".g"));
            gFiles.Add(System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "Local\\FindReplace\\Temp\\" + "FOX_" + System.IO.Path.GetFileName(file).Replace(".fdf", ".g"));
        }
        foreach (string file in gFiles)
        {
            System.IO.StreamReader gFile = new System.IO.StreamReader(file);
            string line = "";
            while ((line = gFile.ReadLine()) != null)
            {
                foreach (string template in templates)
                {
                    if (line.Contains(template) && !templatesInSelection.Contains(template))
                    {
                        templatesInSelection.Add(template);
                    }
                }
            }
        }
        findReplaceGrid.AutoGenerateColumns = false;
        findReplaceGrid.DataSource = templatesInSelection;
    }

I have checked abd the conversion is working just fine. The program, however, throws an exception on the line reading:

System.IO.File.Move("FOX_" + System.IO.Path.GetFileName(file).Replace(".fdf",".g"), System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "Local\\FindReplace\\Temp\\" + "FOX_" + System.IO.Path.GetFileName(file).Replace(".fdf", ".g"));

With the exception:

Additional information: Could not find file 'C:\Users\James.Hughes\Source\Repos\Find-and-Replace-Symbols\Find and Replace Symbols\bin\Debug\FOX_18_83_COD_002.g'.

The conversion utility renames files using this format FOX_%origName%.g hence the string manipulation of the original file path to get the new name.

What I can't understand is why it is attempting to access C:\Users\James.Hughes\Source\Repos\Find-and-Replace-Symbols\Find and Replace Symbols\bin\Debug\FOX_18_83_COD_002.g' when it should be looking in appdata.

I'm totally flummuxed so any help would be appreciated!

Upvotes: 1

Views: 66

Answers (1)

Dygestor
Dygestor

Reputation: 1259

System.IO.File.Move takes 2 arguments: sourceFileName and destinationFileName. You did not specify the path to sourceFile, so it looks for it in the current working directory (bin/Debug). Try specifying the path to the actual location of the file.

File.Move documentation

Upvotes: 1

Related Questions