0

I have a File on my Desktop and I want to get the full Path of the File in my code, should it be on my Desktop or anywhere

My code is looking like this

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GetFullPath
{
    class Program
    {
        static void Main(string[] args)
        {
            string filename = "eMemoExpenseApproval.docx";

            string fullFilePath = Path.Combine(Directory.GetCurrentDirectory(), filename);
            Console.Write("Path : " + fullFilePath);
            Console.Read();
        }
    }
}

Rather than get the full path from Desktop it shows the Path from Visual Studio, which is not suppose to be so, but i get this instead

Path : C:\Users\Administrator\Documents\Visual Studio 2017\Projects\GetFullPath\GetFullPath\bin\Debug\eMemoExpenseApproval.docx

Edit:

this works to get the Path of the file on Desktop

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GetFullPath
{
    class Program
    {
        static void Main(string[] args)
        {
            string filename = "eMemoExpenseApproval.docx";

            string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string fullFilePath = path +"/"+ filename;
            Console.Write("Path : " + fullFilePath);
            Console.Read();
        }
    }
}

Fine but How about other directories?

Hamish
  • 11
  • 1
  • 7
  • 3
    `Environment.GetFolderPath(Environment.SpecialFolder.Desktop);` – Pavel Anikhouski Dec 18 '19 at 09:39
  • 3
    In general, you shouldn't make any assumptions about where `Directory.GetCurrentDirectory()` is pointing when your process starts. – Damien_The_Unbeliever Dec 18 '19 at 09:40
  • As the above comments point to. Read this https://learn.microsoft.com/en-us/dotnet/api/system.environment.getfolderpath?view=netframework-4.8 – Shahid Manzoor Bhat Dec 18 '19 at 09:41
  • The question is: why do you assume that GetCurrentDirectory is the path of the Desktop? –  Dec 18 '19 at 09:43
  • It's unclear what you are asking. Do you mean to search the whole disk for that file name? In that case your code is totally off. – Palle Due Dec 18 '19 at 10:01
  • @PalleDue, yes. i want it to just use the file name, to find whatever directory the file is saved in and then get the full path , for instance, if its in my documents, it fetches the path for the file saved in documents folder, should it be on desktop for instance it gets the full path for desktop – Hamish Dec 18 '19 at 10:16
  • @Hamish, can you tell us some sample inputs and expected output. When you say the file can be anywhere not just on your desktop, then are you implying it needs to search and retrieve ? – Clint Dec 18 '19 at 10:36

2 Answers2

0

Directory.GetCurrentDirectory() actually returns the directory in which the application is executed. If you know that the file is located in your Desktop, you can instead do something like this : string fullFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop, filename));

S_Mindcore
  • 93
  • 1
  • 10
0

As I understand you want to search a limited set of folders for a named file. To do that declare a function like this:

IEnumerable<string> FindInMultipleFolders(string[] folders, string filename)
{   
    var result = new List<string>();
    foreach (var folder in folders)
    {
        var dirs = Directory.GetFiles(folder, filename);
        foreach (String dir in dirs) 
        {
            result.Add(dir);
        }
    }
    return result;
}

And call it with the file name and the folders to search like this:

FindInMultipleFolders(
    new string[] 
    {
        Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
        Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
        @"C:\Some\Other\Folder\I\Would\Like\Searched"
    }, 
    "eMemoExpenseApproval.docx");
}

The file might be in multiple folders, so the function returns an IEnumerable<string>. FindInMultipleFolders only searches the passed folders, not subfolders. If you want subfolders to be searched you should add SearchOption.AllDirectories as a third parameter to GetFiles. Then you could search the whole hard drive with:

FindInMultipleFolders(
    new string[] 
    {
        @"C:\"
    }, 
    "eMemoExpenseApproval.docx");
}
Palle Due
  • 5,929
  • 4
  • 17
  • 32