How to get root path of current working project in Xamarin.Android or Xamarin.Forms project? Forms project is PCL, hence I am trying out in Xamarin.Android project. Tried following things:
string rootPath = AppDomain.CurrentDomain.RelativeSearchPath;
string rootPath = AppDomain.CurrentDomain.SetupInformation.PrivateBinPath;
string rootPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
string rootPath = System.Environment.CurrentDirectory;
string rootPath = System.IO.Directory.GetCurrentDirectory();
Everything is returning null. I want to dynamically create file in particular folder, for that I need root path of my project. How to get it?
Related
I have two asp.net projects in one solution. One is WebForms and the another is a WebApi project.
I am trying to save an image in the root folder of the WebForms using a method in the WebApi. Unfortunately, I am unable to get the BaseDirectory of the WebForms from the WebApi method.
This is what I have tried:
//this returns me only the root folder of the API i.e; D:\Projects\MyApp\API\)
string basePath = System.AppDomain.CurrentDomain.BaseDirectory;
Is it possible to do what I am trying?
I guess you can navigate to you related app if it destination meet some folder tree position requirement using this:
string relativePath = "..\\Web\\";
string basePath = System.AppDomain.CurrentDomain.BaseDirectory;
string webPath = Path.GetFullPath(baseDirectory + relativePath);
if you want to change the ResourceDictionary in code, you have to write a long path, like new Uri(#"pack://application:,,,/MyProject;component/System/Language/Window1_EN.xaml", UriKind.Absolute).
Is there any way I can use by using the file name (Window11_EN.xaml) to get its Path of Project (/System/Language) ?
first of all you need to have property that could store your file path
public string xmlfilepath{get;set;}
now, all you have to do is to get application base directory, and concatenate with your path,
as an example i have created:
static string path = System.AppDomain.CurrentDomain.BaseDirectory;
static string debug = Path.GetDirectoryName(path);
static string bin = Path.GetDirectoryName(debug);
static string projectfolder = Path.GetDirectoryName(bin);
public string xamlFilepath = projectfolder + "\\System\\Language\\Window1_EN.xaml";
or if it is in the bin/debug try using this one
xmlfilepath= System.AppDomain.CurrentDomain.BaseDirectory + "your folder name and file name path will come here";
That's my problem. I have a Window1_EN.xaml resourcedictionary file, like the following picture shows. we can use uri syntax to get the file according to its directory in Solution Explorer. we don't need to copy the folder into Bin/Debug directory. But it need to write it manually. Is the anyway I can code it?
Is there another way to get the root of a wpf application as a string?
Now I'm still using
string path = AppDomain.CurrentDomain.BaseDirectory.Substring(0, (AppDomain.CurrentDomain.BaseDirectory.Length - 10));
This gives me the root as a string, but I assume this is not the right way to do it.
I also tried
string txt = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
but this sends me to root/bin/debug.
I only need the root as a string
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
Another way is:
Environment.CurrentDirectory
if it was not changed.
You can find the file path of the root folder of the startup project in a WPF App like this:
string applicationDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
string rootPath = Directory.GetParent(applicationDirectory).Parent.FullName;
Or to complete your example by getting the file path of the parent folder of the parent folder, you can do this:
string rootPath = Directory.GetParent(txt).Parent.FullName;
UPDATE >>>
In order to access your project Images folder, you can do this:
Path.Combine(Directory.GetParent(applicationDirectory).Parent.FullName, "Images");
You should put the images in a folder of your Visual Studio project called "Images" and set their Build Action to Resource (as shown here).
If you then get a relative image path from your DB, you would create a Pack URI and load a BitmapImage like this:
var imagePath = "Images/SomeImage.jpg"; // actually from DB
var uri = new Uri("pack://application:,,,/" + imagePath);
var bitmap = new BitmapImage(uri);
I am trying to create a project item programmatically. i have this code
string itemPath = currentSolution.GetProjectItemTemplate("ScreenTemplate.zip", "csproj");
currentSolution.Projects.Item(1).ProjectItems.AddFromTemplate(itemPath, name);
currentSolution.Projects.Item(1).Save();
But I would like to create the item in a specified directory inside the project (this creates the item in the root of the project). Is it possible? Thanks for help!
That's roughly how I add my cpp file, should be no different in your case.
The code will add the file under "SourceFiles\SomeFolder" in the project and also in the "Source Files" folder in the project view tree (it should be already there).
Project project = null; // you should get the project from the solution or as active project or somehow else
string fileName = "myFileName.cpp";
string fileRelativePath = "SourceFiles\\SomeFolder\\" + fileName;
// First see if the file is already there and delete it (to create an empty one)
string fileFullPath = Path.GetDirectoryName(project.FileName) + "\\" + fileRelativePath;
if (File.Exists(fileFullPath))
File.Delete(fileFullPath);
// m_applicationObject here is DTE2 or DTE2
string templatePath = (m_applicationObject.Solution as Solution2).ProjectItemsTemplatePath(project.Kind);
ProjectItem folderItem = project.ProjectItems.Item("Source Files");
ProjectItem myFileItem = folderItem.ProjectItems.AddFromTemplate(templatePath + "/newc++file.cpp", fileRelativePath);
Please don't expect the code to compile straight away and run - some checks for invalid state aren't performed here.
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.