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?
Related
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"
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 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'm writing to a text folder which is in a folder within my project but I can't seem to get to it without writing the absolute complete path as it is on my computer which is fine on this computer but when I want to take it elsewhere I can't have that as the drives are different etc.
Here is a screenshot of the lines I'm using to get it to post to the directory on the right.
The file I'm trying to access is in a folder called AdminAccount and is called User.txt. it works fine as you can see from the commented directory link as a direct path but when I try with the directory string in use it does not work.
http://i.imgur.com/hAV55W0.png
Any help how to get around this? I tried all sorts, I tried doing
private string[] getLines = System.IO.File.ReadAllLines(#"\AdminAccount\User.txt");
private string[] getLines = System.IO.File.ReadAllLines(#"..\AdminAccount\User.txt");
No joy.
You can use,
string rootPath = Environment.CurrentDirectory;
string filePath = Path.Combine(rootPath,#"..\..\AdminAccount\User.txt");
private string[] getLines = System.IO.File.ReadAllLines(#filePath);
..\ is used to access a top level folder in the hierarchy. you can keep on adding ..\ to move up in the hierarchy.
Ex:
string path1 = #"C:\Users\Documents\Visual Studio 2010\Projects\Test\Test\bin\Debug"
string newPath = Path.Combine(path1, #"..\..\AdminAccount\User.txt");
new path would return
C:\Users\Documents\Visual Studio 2010\Projects\Test\Test\AdminAccount\User.txt
You just have to set the property "Copy to Output Directory" of the "User.txt" file to "Copy allways" or "Copy if newer".
Now you can read the lines as below
string[] getLines = File.ReadAllLines(
Path.Combine(Application.StartupPath, "AdminAccount", "User.txt"));
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.