Setting a root path to use relative paths from - c#

Is there a way to set a 'root' or a base path to then use relative paths from in C#?
So for example, say I had the path:
C:\Users\Steve\Documents\Document.txt
Could I then use this path, instead of the programs assembly, to be the base path. So this would then allow me to use something like:
..\..\Pictures\Photo.png
Thanks

why not do:
string RootPath = string.empty
#if DEBUG
RootPath = "C:\Users\Steve\Documents\Document.txt"
#else
RootPath = System.Reflection.Assembly.GetAssembly(typeof(MyClass)).Location;
#endif
var newPath = Path.Combine(RootPath, "..\..\Pictures\Photo.png");

Related

How does the system go backward one path

It's current path is C:\Users\USERNAME\OneDrive\Desktop\Pro\Professional
How do i set it to C:\Users\USERNAME\OneDrive\Desktop\Pro
But I don't set by writing string to set it back I want the system to understand where it is and go backwards.
Simply put I want it to be like the cd.. command in Windows CMD.
To get the current directory, use
var currentDirectory = Directory.GetCurrentDirectory();
To get the parent directory, use
var parentDirectory = Path.GetDirectoryName(currentDirectory);
And finally to set the current directory:
Directory.SetCurrentDirectory(parentDirectory);
There is a method in System.IO called Path.GetDirectoryName. this would take your path as input and return the parent directory of the path which you have given.
example:
string path = "C:\Users\USERNAME\OneDrive\Desktop\Pro\Professional";
string parentDirectory = Path.GetDirectoryName(path);
here, the parentDirectory now containes "C:\Users\USERNAME\OneDrive\Desktop\Pro"
if you want to go up multiple leveles you can call multiple times,
example:
string path = #"C:\Users\USERNAME\OneDrive\Desktop\Pro\Professional";
string parentDirectory = Path.GetDirectoryName(Path.GetDirectoryName(path));
the parentDirectory now containes "C:\Users\USERNAME\OneDrive\Desktop"

File not relocating correctly c#

I have my program setup to rename and store a file according to checkbox input. I used another stackoverflow post for my template. Only problem is when I tried setting it up for sub-folders, it never puts it in the correct folder. I have a label folder with two sub folders called L-Labels and B-Labels. The user checks which label type it is and the file gets renamed and placed in the according sub-folder. When I used breakpoint my variables are getting the correct value so I don't see what's wrong I have provided my variables and code for relocating the file. What is causing this to not put it in my sub-folder?
Varibales:
string oldPath = lblBrowseName.Text;
string newpathB = #"C:\Users\Public\Labels\B_Labels";
string newpathL = #"C:\Users\Public\Labels\L_Labels";
Method:
if (rChkBoxBizerba.Checked == true)
{
string newFileName = rtxtBoxNewVersion.Text;
FileInfo f1 = new FileInfo(oldPath);
if (f1.Exists)
{
if (!Directory.Exists(newpathB))
{
Directory.CreateDirectory(newpathB);
}
f1.CopyTo(string.Format("{0}{1}{2}", newpathB, newFileName, f1.Extension));
if (System.IO.File.Exists(lblBrowseName.Text))
System.IO.File.Delete(lblBrowseName.Text);
}
I would say this is the problem:
f1.CopyTo(string.Format("{0}{1}{2}", newpathB, newFileName, f1.Extension));
You declare your path but it doesn't have a trailing directory separator, so when you combine all the parts, as above, the actual result is invalid.
You really should use Path.Combine() to combine parts of paths together, this uses the correct directory separator and makes additional checks.
Try something like this:
// Build actual filename
string filename = String.Format("{0}{1}",newFileName, f1.Extension));
// Now build the full path (directory + filename)
string full_path = Path.Combine(newpathB,filename);
// Copy file
f1.CopyTo(full_path);

Check if directory exists with dynamic path

How can I check if the directory exists with a dynamic path (~) not a fixed path (C:)?
My code:
Soin_Id = Request.QueryString["SoinId"];
string path = #"~\Ordo\Soin_"+Soin_Id+#"\";
if (Directory.Exists(path))
{
ASPxFileManager_Ordo.Settings.RootFolder = path;
}
else
{
ASPxFileManager_Ordo.Settings.RootFolder = #"~\Ordo\";
}
With this condition, it's never true, even though the directory exists.
You need to use Server.MapPath to resolve dynamic path to physical path on server.
if (Directory.Exists(Server.MapPath(path)))
also consider using Path.Combine for concatenation of path.

Path from process name that is NOT running

I want to get full path from process name WITHOUT running the process.In otherwords- where C# gets absolute path when it is executing following :
Process.Start(startInfo);
startInfo does not contain absolute path.
The full path of the executable is resolved through the %PATH% environment variable. You can replicate the behaviour as follows:
var result = Environment.GetEnvironmentVariable("PATH")
.Split(';')
.Select(path => Path.Combine(path, "notepad.exe"))
.FirstOrDefault(path => File.Exists(path));
// result == "C:\\Windows\\system32\\notepad.exe"
Uses standard windows search policy: current folder and folders in the PATH environment variable.
Perhaps I misunderstood, but what about :
var fInfo = new FileInfo(startInfo.FileName);
var fullPath = fInfo.FullName;
?

How Can I Remove The 'file:\\' From the Return Value of Path.GetDirectoryName() in C#

string path = Path.GetDirectoryName(
Assembly.GetAssembly(typeof(MyClass)).CodeBase);
output:
file:\d:\learning\cs\test\test.xml
What's the best way to return only d:\learning\cs\test\test.xml
file:\\ will throw exception when I call doc.Save(returnPath) ,however doc.Load(returnPath); works well. Thank you.
string path = new Uri(Assembly.GetAssembly(typeof(MyClass)).CodeBase).LocalPath;
If you want the directory of the assembly of that class, you could use the Assembly.Location property:
string path = Path.GetDirectoryName(Assembly.GetAssembly(typeof(MyClass)).Location);
This isn't exactly the same as the CodeBase property, though. The Location is the "path or UNC location of the loaded file that contains the manifest" whereas the CodeBase is the " location of the assembly as specified originally, for example, in an AssemblyName object".
System.Uri uri = new System.Uri(Assembly.GetAssembly(typeof(MyClass)).CodeBase);
string path = Path.GetDirectoryName(uri.LocalPath);
My first approach would be like this...
path = path.Replace("file://", "");
use the substring string method to grab the file name after file:\

Categories

Resources