I have a C# application which assume it runs from bin directory
string current_directory = Directory.GetCurrentDirectory(); //get current directory, this is the bin dir
string parent_dir = Directory.GetParent(current_directory).ToString();// this is parent of bin dir
string _Config1 = parent_dir + "\\config\\x.cfg";
string _Config2 = parent_dir + "\\config\\y.cfg";
string _Log = parent_dir + "\\log\\z.log";
The problem is for some reasons the user can not go to the bin directory and run the app (just type "application_name"). He has to run it using path (ie d:\blah\1\blah\2\blah\3\bin\application_name)
when he does this he gets
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)
so either I have to capture the path he uses to run the program and use it in my program or somehow make my application to be able to run using path.
You can use AppDomain.CurrentDomain.BaseDirectory.
Use the Application.StartupPath for WinForms.
You can use reflection:
var path = System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().GetName().GetCodebase );
Related
I'm making an installer using IExpress to unpack the files, create a folder and move the files to the folder.
However, when choosing which program to run upon installation I can only get it to work using a batch-file:
#ECHO OFF
MD C:\PlugInFolder
MOVE /Y "%USERPROFILE%\AppData\Local\Temp\IXP000.TMP\*.png" C:\PlugInFolder
MOVE /Y "%USERPROFILE%\AppData\Local\Temp\IXP000.TMP\PlugIn.dll" C:\PlugInFolder
MOVE /Y "%USERPROFILE%\AppData\Local\Temp\IXP000.TMP\PlugIn2021.addin" C:\ProgramData\Autodesk\Revit\Addins\2021
MOVE /Y "%USERPROFILE%\AppData\Local\Temp\IXP000.TMP\PlugIn2022.addin" C:\ProgramData\Autodesk\Revit\Addins\2022
Is it possible to run an exe file instead? I've tried the following C#-code (for one of the files only) but it only creates the folder and doesn't move the files:
// Creating paths
string path = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
string folderName = "PlugInFolder";
string pathString = Path.Combine(path, folderName) + "\\PlugIn.dll";
string tempName = Path.GetTempPath() + "IXP000.TMP\\";
string fileName = "PlugIn.dll";
string filePath = tempName + fileName;
// Creating new directory
Directory.CreateDirectory(pathString);
// Moving files from temp folder
File.Move(filePath, pathString);
There are a few typos in your code. Here's what could work, correctly using Path.Combine which is a powerful command:
// Creating paths
string source = Path.Combine(Path.GetTempPath(), "IXP000.TMP");
string dest1 = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
string dest2 = "PlugInFolder";
string dest = Path.Combine(dest1, dest2);
// Creating new directory
// Directory.CreateDirectory(dest);
// Don't create the directory, because we will use Directory.Move
// and it would raise an exception "Directory already exists"
// Moving files from temp folder to destination
// File.Move is meant to move one file at a time.
// For an entire directory, use Directory.Move which can also be used for renaming the directory.
Directory.Move(source, dest);
And, by the way, is it a choice to put an application in the user profile dir? Otherwise, you may use:
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
which would be C:\Users\yourName\AppData\Roaming for example in Windows 11.
I am trying to make a file path inside of the folder above the executable. For instance, I am wanting the variable TAGPATH to be the filepath to an executable in the folder C:\User\ApplicationFolder\tag_rw\tag_rw.exe while the application is in C:\User\ApplicationFolder\AppFiles. I want the application to be portable, meaning no matter the folder names it will retrieve the filepath of the application's executable then go to the parent folder and navigate into tag_rw\tag_rw.exe.
I basically want string TAGPATH = #"path_to_appfolder\\tag_rw\\tag_rw.exe"
Here is what I have tired so far (using the first answer How to navigate a few folders up? ):
string appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
string TAGPATH = System.IO.Path.GetFullPath(System.IO.Path.Combine(appPath, #"..\"));
I am getting a run-time error ArgumentException with the description URI formats are not supported.
Is there an easier/better way to go about this?
Thank you!
Can you try this?
static void Main(string[] args)
{
string cur = Environment.CurrentDirectory;
Console.WriteLine(cur);
string parent1 = Path.Combine(cur, #"..\");
Console.WriteLine(new DirectoryInfo(parent1).FullName);
string parent2 = Path.Combine(cur, #"..\..\");
Console.WriteLine(new DirectoryInfo(parent2).FullName);
Console.ReadLine();
}
Navigation is limited to absolute and relative types. I think you mean to navigate to parent directory regardless of whole application location.
Maybe you try relative path
string TAGPATH = "..\\tag_rw\\tagrw.exe"
I am trying to get a folder path in my C drive that I did not want to hard code with my program.
I know that I can use string path = System.AppDomain.CurrentDomain.BaseDirectory; However its give me : C:\FACE\Camera\Camera\bin\Debug . Which I want to only have C:\FACE\ as I want to initialise something in the FACE folder but outside of my Camera folder. I do not wish to hardcode the file path. Is it possible to do it? Thanks for the help!
You could use the method Directory.GetParent for this purpose:
Directory.GetParent("here you will pass you path");
Update
For your case, you could take that you want as below:
string projectPath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
string solutionPath = Directory.GetParent(projectPath).Parent.FullName;
string basePath = Directory.GetParent(solutionPath).FullName;
The variable basePath contains that you want.
System.AppDomain.CurrentDomain.BaseDirectory always returns the path of executable assembly.You can use DTE if you are using web application
I currently have a statement as follows:
string dir = "C:\\Users\\Limited\\Desktop\\";
Although I would like it to be specified as a directory within the work directroy e.g.
workingpath/myfolder
Can this be done?
I assumed you could just use a relative path, i.e. "myfolder", but you can get and use the application path and append the subdirectory:
string appPath = Path.GetDirectoryName(Application.ExecutablePath);
http://www.csharp-examples.net/get-application-directory/
Just use the relative path to the application.
Unless your path begins with a (drive letter or back)slash¹, it is interpreted as relative to the current working directory. So "myfolder\\" would be a relative directory.
¹In MS-DOS, and emulated by cmd.exe, it's possible to have a path relative to the current directory on another drive.
const string subDir = "test_dir";
string appPath = Path.GetDirectoryName(Application.ExecutablePath);
string targetPath = Path.Combine(appPath, subDir);
I convert my wave file into a mp3 file by the following code:
internal bool convertToMp3()
{
string lameEXE = #"C:\Users\Roflcoptr\Documents\Visual Studio 2008\Projects\Prototype_Concept_2\Prototype_Concept_2\lame\lame.exe";
string lameArgs = "-V2";
string wavFile = fileName;
string mp3File = fileName.Replace("wav", "mp3");
Process process = new Process();
process.StartInfo = new ProcessStartInfo();
process.StartInfo.FileName = lameEXE;
process.StartInfo.Arguments = string.Format("{0} {1} {2}", lameArgs, wavFile, mp3File);
process.Start();
process.WaitForExit();
int exitCode = process.ExitCode;
if (exitCode == 0)
{
return true;
}
else
{
return false;
}
}
This works, but now I'd like to not use the absolut path to the lame.exe but a relative path. I included a lame.exe in the folder /lame/ on the root of the project. How can I reference it?
If you have rights to distribute the file with your application, then one way of doing it would be to include the .exe file as an item in your C# project, with
Build Action = None
Copy to Output Directory = Copy if newer
You can then just use
string lameEXE = #"lame.exe"
Assuming your binary app is in Debug folder and the lame folder is in the main project directory:
string lameEXE = #"..\..\lame\lame.exe
Hovewer, folders structure will be probably different in your release version.
Directly using a path relative to the application directory is a bad idea since the working directory might not be identical to the application directory. This can lead to bugs and security holes. For example you might execute files in untrusted directories.
Some cases where the working directory isn't identical to the application directory:
The user opens a file from the explorer. Then the working directory is the directory where the file is in
Depending on the settings Common Dialogs(OpenFileDialog, SaveFileDialog) change the working directory.
So you should expand it relative to the project directory. Unfortunately I know of no clean library function which does that for you. In simple cases this can be done by concatenating the relative path to the application directory:
string appDir = Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) + "\\";
const string relativePath=#"..\..\lame.exe";//Whatever relative path you want
string absolutePath=appDir+relativePath;
...
process.StartInfo.FileName =absolutePath;
...