How do I convert a path from my drive to an application path? Currently I am creating and accessing files on my hard disk (eg: "D:\MyFolder\MyDoc.doc"). I want this path inside my console application folder. I know I can use Server.MapPath for ASP.NET applications. What about console applications?
            Asked
            
        
        
            Active
            
        
            Viewed 1,395 times
        
    2
            
            
         
    
    
        Michael Petrotta
        
- 59,888
- 27
- 145
- 179
 
    
    
        shakz
        
- 629
- 3
- 14
- 38
- 
                    1http://stackoverflow.com/questions/837488/how-can-i-get-the-applications-path-in-net-in-a-console-app – Habib Sep 27 '12 at 05:47
- 
                    What do you mean with "convert"? – Dennis Sep 27 '12 at 05:55
- 
                    Sorry,Actually not convert. I just want this inside application folder. Currently I am using this in hard drive. – shakz Sep 27 '12 at 05:56
- 
                    Using System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) ; I can get the debug folder. But how to retrieve the folder and files that I created inside the application. – shakz Sep 27 '12 at 06:12
- 
                    Are you trying to take an absolute path like `D:\something\something` and convert it to a path relative to your applications current working directory like ` ..\..\something\somthing\ ` ? – Alex Gelman Sep 27 '12 at 06:15
- 
                    No.Actually for development purpose i saved my files on harddisk. Now i want to deploy it to server. So I want to change the file paths to app path. – shakz Sep 27 '12 at 06:18
2 Answers
3
            EDITED
If you want to read file from your current folder
System.IO.FileStream stream = System.Reflection.Assembly.GetExecutingAssembly().GetFile("filename");
or you want to get directory path
 string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
            var directory = System.IO.Path.GetDirectoryName(path);
            var parentdir = System.IO.Directory.GetParent(directory);
 
    
    
        Anant Dabhi
        
- 10,864
- 3
- 31
- 49
- 
                    Is it possible to access a new folder which is created before the bin folder? – shakz Sep 27 '12 at 06:41
2
            
            
        To avoid hardcoding path file, put MyDoc.doc in execution folder, then you can get execution folder by  using Directory.GetCurrentDirectory():
string directory = Directory.GetCurrentDirectory();
string fileName = Path.Combine(directory, "MyDoc.doc");
Other alternative:
string path = Assembly.GetExecutingAssembly().Location;
string directory = Path.GetDirectoryName(path);
Or:
string directory = AppDomain.CurrentDomain.BaseDirectory;
Or:
string directory = Path.GetDirectoryName(Application.ExecutablePath);
To get bin folder:
 var bin = Directory.GetParent(directory ).Parent.FullName;
 
    
    
        cuongle
        
- 74,024
- 28
- 151
- 206
- 
                    I am using vs 2005. So I cannot use var keyword.What to do?string will work? – shakz Sep 27 '12 at 06:20
- 
                    But these codes are returning me debug folder.D:\Projects\WebDataReceiver\WebDataReceiver\bin\Debug. Isnt it possible to get bin folder itself? – shakz Sep 27 '12 at 06:30