How to get files in a relative path in C#
To make sure you have the application's path (and not just the current directory), use this:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.getcurrentprocess.aspx
Now you have a Process
object that represents the process that is running.
Then use Process.MainModule.FileName
:
http://msdn.microsoft.com/en-us/library/system.diagnostics.processmodule.filename.aspx
Finally, use Path.GetDirectoryName
to get the folder containing the .exe:
http://msdn.microsoft.com/en-us/library/system.io.path.getdirectoryname.aspx
So this is what you want:
string folder = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + @"\Archive\";
string filter = "*.zip";
string[] files = Directory.GetFiles(folder, filter);
(Notice that "\Archive\"
from your question is now @"\Archive\"
: you need the @ so that the \
backslashes aren't interpreted as the start of an escape sequence)
Hope that helps!
Get relative Path of a file C#
You can use Directory.GetParent
and its Parent
member
string path = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
Will go two levels up the path tree and return "C:\TFS\MySolution\Project1"
.
Copy file using relative path
To execute the File.Copy
the source and destination will be a valid file path. in your case the destination is a folder not File. in this case you may get some exception like
Could not find a part of the path 'F:\New folder'
While executing the application, the current directory will be the bin
folder. you need to specify the relative path from there. Let my program/subfolder
be the folders in your solution, so the code for this will be like this:
string sourcePath = "../../my program/subfolder/what i want to copy.txt";
string destinationPath = @"C:\Targetlocation\copyFile.txt"
File.Copy(sourcePath, destinationPath );
Where ../
will help you to move one step back from the current directory. One more thing you have to care is the third optional parameter in the File.Copy
method. By passing true
for this parameter will help you to overwrite the contents of the existing file.Also make sure that the folder C:\Targetlocation
is existing, as this will not create the folder for you.
How to get files from a directory with relative path Directory.GetFiles(sPath)
You should you Path.Combine to construct absolute path or you can change Current Directory so relative path points to location you need.
// Building c:\my\home\work\FilesDirectory
var absolutePath = Path.Combine(@"c:\my\home\toys\", @"..\work\FilesDirectory" );
Note: you need to know what location the path is relative to. I.e. if it relative to executable use Assembly.GetEntryAssembly().Location
as base path.
How to get relative path of a file in visual studio?
When it is the case that you want to use any kind of external file, there is certainly a way to put them in a folder within your project, but not as valid as getting them from resources. In a regular Visual Studio project, you should have a Resources.resx
file under the Properties
section, if not, you can easily add your own Resource.resx
file. And add any kind of file in it, you can reach the walkthrough for adding resource files to your project here.
After having resource files in your project, calling them is easy as this:
var myIcon = Resources.MyIconFile;
Of course you should add the using Properties
statement like this:
using <namespace>.Properties;
C# Application Relative Paths
You don't need to specify full directory everytime, relative directory also work for C#, you can get current directory using following way-
Gets the current working directory of the application.
string directory = Directory.GetCurrentDirectory();
Gets or sets the fully qualified path of the current working directory.
string directory = Environment.CurrentDirectory;
Get program executable path
string directory = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
Resource Link 1 Resource Link 2
Related Topics
String Concatenation VS String Builder. Performance
C# - Fill a Combo Box with a Datatable
Nunit VS. Visual Studio 2008's Test Projects for Unit Testing
What Is the Static Variable Initialization Order Across Classes in C#
Binding Listbox to List<Object> in Winforms
How to Return an Anonymous Type from a Method
Arbitrary-Precision Decimals in C#
How to Bind Crystal Report to Manually Created Dataset
Get SQL Query from Linq to SQL
Set Default Global JSON Serializer Settings
Why Can't I Assign a List<Derived> to a List<Base>
Gps Socket Communication (Concox)
JSON.Net Serialize by Depth and Attribute
How to Provide Custom Cast Support for My Class