I'm creating an extension for visual studio 2012 and am having a hard time finding the location of the arbitrary file that the extension is running behind. Does anyone have a good way of doing this through the extension? Maybe with reflection or some other sort of Path method?
You can do this:
Assembly.GetExecutingAssembly().Location
Then you need to look at the the functions in Path to find the directory. I think its one of these:
Path.GetDirectoryName
Path.GetPathRoot
I used the path extensions
Works great on https://dotnetfiddle.net/
using System.IO;
var path = "/test/test2/test.txt";
Console.WriteLine($"GetFileName {Path.GetFileName(path)}");
Console.WriteLine($"GetFullPath {Path.GetFullPath(path)}");
Console.WriteLine($"GetDirectoryName {Path.GetDirectoryName(path)}");
// correct exact path without filename
Console.WriteLine($"dirPath {path.Substring(0, path.Length - Path.GetFileName(path).Length)}");
/*
Prints:
GetFileName test.txt
GetFullPath /test/test2/test.txt
GetDirectoryName /test/test2
dirPath /test/test2/
*/
Note
the path separator has to be native to your machine. E.g. /test/test2/test.txt won't work on windows while \test\test2\test.txt will
string executingtitle = _applicationObject.Solution.FullName;
string[] title = executingtitle.Split( ',' );
string filename = title[0];
string filepath = Path.GetFullPath(filename);
One of my co-workers helped me out. Here's the code for future reference.
Related
I've got an absolute path available to me. Say: C:/Program/CoreFiles/Folder1/Folder2/File.txt.
I need to copy that file to C:/Program/Projects/ProjectName/ but it needs to keep Folder1/Folder2/File.txt intact. So the end result should be C:/Program/Projects/ProjectName/Folder1/Folder2/File.txt.
My first attempt at solving this was to try and get the relative path between 2 absolute paths. Found Path.GetRelativePath(string, string) which obviously didn't help as it wasn't meant for WinForms. It would mess up anyway as the final result would be C:/Program/Projects/ProjectName/CoreFiles/Folder1/Folder2/File.txt.
The target directory is empty and I don't know the relative path to copy beforehand other than somehow getting that info out of the absolute path. Since File.Copy won't create folders that don't exist yet, I need to create them first. So how do I get the path that leads up to the file from the CoreFiles directory out of the absolute path?
The only working solution I can come up with is using regex to just replace CoreFiles with Projects/ProjectName in the path string and work with that. But that somehow seems the wrong approach.
Since you can't use Path.GetRelativePath. I suggest looking at another answer that describes how to do this yourself.
Like here...
How to get relative path from absolute path
Using the method in that answer, you can do the rest of your task as shown below.
string sourcePath = "C:/Program/CoreFiles/Folder1/Folder2/File.txt";
string sourceRoot = "C:/Program/CoreFiles/";
string destinationRoot = "C:/Program/Projects/ProjectName/";
// Use built-in .NET Path.GetRelativePath if you can. Otherwise use a custom function. Like here https://stackoverflow.com/a/340454/1812944
string relativePath = MakeRelativePath(sourceRoot, sourcePath);
// Combine the paths, and make the directory separators all the same.
string destinationPath = Path.GetFullPath(Path.Combine(destinationRoot, relativePath));
// Create nested folder structure for your files.
Directory.CreateDirectory(Path.GetDirectoryName(destinationPath));
// Copy the file over.
File.Copy(sourcePath, destinationPath);
I want to convert "C:\Program Files\x\y\z" to "C:\%ProgramFiles%\x\y\z OR "C:\Progra~1\x\y\z"(DOS short path). How to do this C#.Net?
To convert long path to DOS short path I have tried solution posted here however on my Windows 10 it is not working. Any hints?
Update - Please note, path may or may not exist physically on disk. I am looking a solution which can convert any arbitrary path string to above formats. The solution using GetShortPathName(...) works only if the LongPath is physically exist on disk.
If you have a string put into your app such as "c:\program files\x\y\z" and you know it's the wrong place, but don't know where the right place is, you're going to have to make the assumption that c:\program files means "the program files folder" and perform a string replacement with the result of Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) or Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) depending on which you assert it to be
var wrongPath = #"c:\program files\x\y\z";
var actualPath = wrongpath.Replace(
#"c:\program files",
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
);
For conversion to a short path, try something like: Method to convert a long path name to a short path returns a null
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
The error I am receiving upon executing my code is: 'ArgumentException was unhandled. Illegal characters in path.'
I am using the following code to access my .xml file.
string appPath = Path.GetDirectoryName(System.Environment.CommandLine);
FileInfo fi = new FileInfo(appPath + #"\Books.xml");
I am doing this for a console application, as opposed to a WinForm. I have been exhaustively googling, as well as searching SO, for some time.
This is part of a homework project. This is the only problem I am running into however.
string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
FileInfo fi = new FileInfo(Path.Combine(appPath, "Books.xml"));
System.Environment.CommandLine does not return a path - it returns the value of the command line that was executed to run the application.
You probably need to use Assembly.GetExecutingAssembly().Location (as Furqan Safdar posted in his answer) instead.
The format of the EXE path returned by CommandLine is funky, so you need to do something like this:
string appPath = Path.GetDirectoryName(System.Environment.CommandLine.Trim('"', ' '));
That should work.
Use this code to get application directory:
var rootDirectory = AppDomain.Current.BaseDirectory;
Good luck!
I have one folder named "Images" in my project. I want to get the path of the Image folder so that I can browse that and get the files.
I'm using below piece of code for my above requirement and it is working fine.
string basePath = AppDomain.CurrentDomain.BaseDirectory;
basePath = basePath.Replace("bin", "#");
string[] str = basePath.Split('#');
basePath = str[0];
string path = string.Format(#"{0}{1}", basePath, "Images");
string[] fileEntries = Directory.GetFiles(path);
foreach (string fileName in fileEntries)
listBox.Items.Add(fileName);
Just want to know like is there any elegant way of doing this? What are the best ways of getting the folder path?
This is what i usually use:
string appDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Note that this returns the directory of the assembly that contains the currently executing code (i assume this is your main executable).
To get the parent directory of the resulting path, you can also use
Path.GetDirectoryName(appDirectory);
I would advice against depending on the Visual Studio project structure in your code, though. Consider adding the images folder as content to your application, so that it resides in a subdirectory in the same directory as your executable.
If you are just trying to reference a directory with a fixed relationship to another, then you can just use the same .. syntax you'd use at the command line?
You should also use the methods in the Path class (eg Path.Combine) rather than all that string manipulation.