Application path [duplicate] - c#

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How can I get the application's path in .NET in a console app?
How can I get application path? (folder the executable is in)

Determine the Executing Application's Path
path = System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );

From Brad Abrams site, you might want to look at this code:
Console.WriteLine (Environment.GetCommandLineArgs()[0]);
Or, from comments in that link:
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;

Related

Path of a folder inside my project [duplicate]

This question already has answers here:
get path for my .exe [duplicate]
(4 answers)
Closed 5 years ago.
So I'm trying to find a way of accesing the path of the folder to copy files but I can't find a way to do it
string folderpath = "C:\Users\Marc Vila\Downloads\Version 3.5\WindowsForm1\WindowsForm1/Resources";
I don't want to do that because as soon as I move it it gives me an error, anyone knows a more efficient way to do it?
Thanks and regards
Note: I've tried System.IO but it gives me a lot of trouble
You need to get the assembly's path and then build a relative path on top of that.
Take the code from this answer and then use that to combine the paths:
var folderPath = Path.Combine(AssemblyDirectory, "Resources");

Check if [path to exe] has running instance? [duplicate]

This question already has answers here:
Check if a specific exe file is running
(8 answers)
Closed 5 years ago.
Is there a way to check (using C#) whether there is an instance of an EXE that is refered to by path running? If so, please provide the method of doing so. Thank you in advance.
Example: Is there a way to find out if "C:\foo\bar.exe" has a running instance, by refering to the EXE as "C:\foo\bar.exe"?
Do you want something like that;
public List<Process> GetProcessesByFileName(string fileName)
{
return Process.GetProcesses().Where(x => x.MainModule.FileName == fileName).ToList();
}

Why does Path.Combine ignore the first part of a ftp server path? [duplicate]

This question already has answers here:
Why does Path.Combine not properly concatenate filenames that start with Path.DirectorySeparatorChar?
(16 answers)
Closed 5 years ago.
I have two parts of a ftp server path. Let's name the /one and /two.
The full path I want to achieve is /one/two.
When I use System.IO.Path.Combine("/one", "/two"); the output is "/two".
Why is that so? I expected it to be "/one/two" as you would normally with windows paths.
Is this by design, wrong usage by me or a bug in the .net class (unlikely)?
(How) can I use System.IO.Path functionality to produce "/one/two"?
If path2 contains an absolute path, this method returns path2.

How to determine the path that was used to start the current application? [duplicate]

This question already has answers here:
How can I get the application's path in a .NET console application?
(30 answers)
Closed 9 years ago.
How can we figure out the filesystem location of the running application?
If you work on Console use this
AppDomain.CurrentDomain.BaseDirectory
or on Windows Forms use
Application.StartupPath
Environment.GetCommandLineArgs()[0];

Path combine question [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Path.Combine for Urls?
I have a root directory like http://localhost/
I have a file name call sample.jpg
when I use Path.Combine(root, file), I get something like http://localhost\sample.jpg, I am wondering if I can get http://localhost/sample.jpg.
Path.Combine is designed for file system paths, not URLs, so I don't think it will give you what you desire in this case. You can always do the Path.Combine, followed by a String.Replace("\", "/") to correct your URL.

Categories