Click here to Skip to main content
15,885,119 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I know that there are many topics about using relative path, but I'm not able to obtain what I'm looking for. I have an app which gets some file from a location on my hdd. The files are located in C:\ReportDesigner\Reports. What I want is to be able to get the files if the folder structure ReportDesigner\Reports is located on whatever partition. For example, my reports could be stored on D:\ReportDesigner\Reports. How should my relative path look like?

What I have tried:

foreach (string file in System.IO.Directory.GetFiles(@"/../ReportDesigner/Reports"))
            {
                retList.Add(System.IO.Path.GetFileNameWithoutExtension(file));
            }

Now my reports are located in D:\ReportDesigner\Reports. For the code shown above, I get tthe exception System.IO.DirectoryNotFoundException: 'Could not find a part of the path 'C:\ReportDesigner\Reports'.'
Posted
Updated 28-Aug-22 22:48pm
Comments
Richard MacCutchan 29-Aug-22 4:25am    
Do not hard code your paths in the application. Either pass the required path on the command line, or use a settings file.
PIEBALDconsult 29-Aug-22 21:08pm    
Don't go looking for files. Have the user tell you where they are.

You can't use a relative path to switch partitions: a relative path is on the current partition, up or down the tree structure from the current folder.
So if you have a structure like this:
C:\
   FilesA\
          Sub1\
                SubA1Sub1    
          Sub2\
                SubA2Sub1    
   FilesB
          Sub1\
                SubB1Sub1    
          Sub2\
                SubB2Sub1

you can use ".." to go upward from C:\FilesA\Sub1 and then into C:\FilesA\Sub2:
..\Sub2

Or to FilesA:
..
Or the root folder:
..\..
But you cannot move "above" the root folder.

You would have to examine the path yourself, and substitute as necessary.
 
Share this answer
 
Ideally, I would make the report path a user setting and save it in whatever persistence mechanism you use so I do not have to find the files every time. I would also place checks to make sure no external tampering has been made to this directory.

If I just have to get this work:

File searches are always done in the current volume. You can actually set current volume for the application by changing it's current working directory. This should be done with utmost care as any other current directory related operations will be affected by this. With this out of way, if I just have to get it to work, I will do this:

C#
var logicalDrives = System.IO.Directory.GetLogicalDrives();

            Parallel.ForEach<string>(logicalDrives, (x) => { new ParallelSearcher { }.SearchFiles(x); });


And the search files would look like this:

C#
public class ParallelSearcher
        {
            internal static ConcurrentBag<string> filePaths = new ConcurrentBag<string>();

            internal void SearchFiles(string drive)
            {
                try
                {
                    System.IO.Directory.SetCurrentDirectory(drive);

                    var results = System.IO.Directory.EnumerateFiles(@"..\ReportDesigner\Reports");

                    foreach (var result in results)
                    {
                        filePaths.Add(result);
                    }
                }
                catch (System.IO.DirectoryNotFoundException ex)
                {
                    // This volume does not have the folder you are looking for. Handle it.
                }
            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900