0

The C# program I am writing needs to access a few XML files and a txt log file, stored in an external folder. What I wanted to do was to drop these files in a folder called ConfigFiles, and then just drop that ConfigFiles folder into the Visual Studio project folder, so that no matter what computer I run the program from, it will be able to access the files. I have not gotten the relative path to work yet. How should I set this up?

I tried:

        //1)Make sure config folder exist and start application Logging
        if (!Directory.Exists(FFXIVFolderPath))
        {
            var appRoot = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            var fullPath = Path.Combine(appRoot, FFXIVFolderPath);

            var message = "FFXIV config directory (" + fullPath + ") does not exist, please select new folder...";
            Globals.Instance.ShowMessage(message);

            // Show the FolderBrowserDialog
            var folderBrowserDialog1 = new FolderBrowserDialog();
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                FFXIVFolderPath = folderBrowserDialog1.SelectedPath;
            }
            else {Application.Exit();}

            MemoryApi.SetForegroundWindow(Handle);
        }

UPDATE (MY SOLUTION): I ended up deciding that I wanted my resource files to be in the "special folder" Documents, under the current user (the users should be able to modify them). This is my code in case someone needs to do the same:

     private const string FFXIVFolder = @"My Games\XYZ Game\Bot Config Files";
     public static string FFXIVFolderPath { get; private set; }

     private void MainForm_Load() {
        //1)Make sure config folder exist and start application Logging
        string documentsDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        FFXIVFolderPath = Path.Combine(documentsDir, FFXIVFolder);
        if (!Directory.Exists(FFXIVFolderPath))
        {
            var message = "FFXIV config directory (" + FFXIVFolderPath + ") does not exist, please select new folder...";
            Globals.Instance.ShowMessage(message);

            // Show the FolderBrowserDialog
            var folderBrowserDialog1 = new FolderBrowserDialog();
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                FFXIVFolderPath = folderBrowserDialog1.SelectedPath;
            }
            else
            {
                Application.Exit();
                return;
            }

            MemoryApi.SetForegroundWindow(Handle);
        }
        Globals.Instance.LoggerDictionary.Add("ApplicationLog", new Logger("ApplicationLog"));
     }
Magnus
  • 6,791
  • 8
  • 53
  • 84
  • Please check this .. you may need to write click on your config files and set the Copy Local property to True in visual studio. if this solves your problem please let me know I will add this as an answer – kishore V M Sep 15 '14 at 16:43
  • Rightclick -> properties -> References Tab – kishore V M Sep 15 '14 at 16:51
  • @kishoreVM Kishore, I just want to set a relative path in my code, and have the files in a folder under my project folder. Any idea? – Magnus Sep 16 '14 at 08:09
  • I think, At the time of checking, Directory.Exists(FFXIVFolderPath) this FFXIVFolderPath should hold the complete path. And Where you are calculating the full path var fullPath = Path.Combine(appRoot, FFXIVFolderPath); here FFXIVFolderPath should have the relative path . i.e. if you calculate full path prior to DIrectory Exists then this code is not needed inside the if condition – kishore V M Sep 16 '14 at 09:01
  • For formulating the relative path please refer the following links http://stackoverflow.com/questions/16906753/local-paths-of-files-contained-in-a-solution – kishore V M Sep 16 '14 at 09:02
  • And this link too .. http://stackoverflow.com/questions/12698599/getting-relative-path-of-file – kishore V M Sep 16 '14 at 09:03
  • If this solves your problem please let me know I will add this as an answer. – kishore V M Sep 16 '14 at 09:03
  • I ended up using code to find the Documents folder automatically instead, and then added some folders to that, please see my update above. You did answer my question though, so if you would like to add an answer i can mark it as accepted... – Magnus Sep 17 '14 at 14:36
  • Thank you for updating your solution. As suggested I have copied my comments as answer. Please mark it as as accepted. – kishore V M Sep 17 '14 at 18:01

1 Answers1

0

I think, At the time of checking, Directory.Exists(FFXIVFolderPath) this FFXIVFolderPath should hold the complete path. And Where you are calculating the full path var fullPath = Path.Combine(appRoot, FFXIVFolderPath); here FFXIVFolderPath should have the relative path . i.e. if you calculate full path prior to DIrectory Exists then this code is not needed inside the if condition.

For formulating the relative path please refer the following links.

  1. Link 1

  2. Link 2

Community
  • 1
  • 1
kishore V M
  • 818
  • 6
  • 13