There is something weird with the way I add to registry run.
When I use
private static string AppPath = new Uri((System.Reflection.Assembly.GetExecutingAssembly().CodeBase)).LocalPath;
to set the path in run registry it worked fine, but if folder name is "c#" for example the added key will be cut before #
so should be :
c:/desktop/c#/myprogram.exe
but it's
c:/desktop/c
What's the problem can you guys help?
I think there is an issue with the Uri escape symbols. Try this:
string AppPath = System.Reflection.Assembly.GetExecutingAssembly ().Location;
I can't duplicate what you're seeing. I think maybe you're missing some information:
var uri = new Uri("c:/desktop/c#/myprogram.exe");
string raw = uri.ToString(); // "file:///c:/desktop/c%23/myprogram.exe"
string local = uri.LocalPath; // "c:\desktop\c#\myprogram.exe"
Are you sure about what's coming out of the Codebase property there?
This happens because the # character gets encoded in the Uri and becomes %23 instead.
I'm not sure why you want to use Uri to get the location of the executable. There is a better way (as nightsnaker posted).
However, if you must use an Uri (for whatever reason), you can get the full path by doing something like this:
string s = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
Uri u = new Uri(s);
string local = u2.LocalPath+u2.Fragment.Replace('/','\\');
Related
I am working on UWP, I want to store paths somewhere, and I found ApplicationData.LocalSettings can do it.
But I got an error when I stored the share folder path through it because of the \ and I don't know why. Is there any good solution to it?
Here is the code,just simple:
var path = #"\\192.168.1.1\test";
ApplicationData.Current.LocalSettings.Values.Keys.Contains<string>(path);
ApplicationData.Current.LocalSettings.Values[path] = xxx;
The contains function and add function also throw COMException
UWP ApplicationData LocalSettings
We could reproduce this problem, it looks "\" char is not support but not "", for this scenario, the better way is using "path" string as key to replace path value-key. And I will report this problem.
ApplicationData.Current.LocalSettings.Values["path"] = xxxx;
Or replace the header \\ with empty.
var path = #"192.168.1.1\test";
I tried your code. LocalSettings cannot use path as a key since it contains the \ symbol.
For example,
String path = #"&&192.168.1.1&test"; // I replaced every \ with &
ApplicationData.Current.LocalSettings.Values[path] = "your string";
will save your string correctly.
I have xml files that contain href file paths to images (e.g. "....\images\image.jpg"). The hrefs contain relative paths. Now, I need to extract the hrefs to the images and turn them into absolute paths in the file system.
I know about the GetFullPath method, but I tried it and it only seems to work from the CurrentDirectory set, which appears to be C: so I don't see how I could use that. And still, I have the absolute path of the file containing the hrefs, and the href relative paths, so since it is a simple task for me to count back the number of "....\" parts based on the absolute path of the containing file, it seems there must be a way to do this programmatically as well.
I'm hoping there's some simple method I just don't know about! Any ideas?
string exactPath = Path.GetFullPath(yourRelativePath);
works
Assuming you know the real directory the XML file lives in use Path.Combine, e.g.
var absolute_path = Path.Combine(directoryXmlLivesIn, "..\images\image.jpg");
If you want to get back the full path with any ..'s collapsed then you can use:
Path.GetFullPath((new Uri(absolute_path)).LocalPath);
This worked.
var s = Path.Combine(#"C:\some\location", #"..\other\file.txt");
s = Path.GetFullPath(s);
It`s best way for convert the Relative path to the absolute path!
string absolutePath = System.IO.Path.GetFullPath(relativePath);
You can use Path.Combine with the "base" path, then GetFullPath on the results.
string absPathContainingHrefs = GetAbsolutePath(); // Get the "base" path
string fullPath = Path.Combine(absPathContainingHrefs, #"..\..\images\image.jpg");
fullPath = Path.GetFullPath(fullPath); // Will turn the above into a proper abs path
Have you tried Server.MapPath method. Here is an example
string relative_path = "/Content/img/Upload/Reports/59/44A0446_59-1.jpg";
string absolute_path = Server.MapPath(relative_path);
//will be c:\users\.....\Content\img\Upload\Reports\59\44A0446_59-1.jpg
This worked for me.
//used in an ASP.NET MVC app
private const string BatchFilePath = "/MyBatchFileDirectory/Mybatchfiles.bat";
var batchFile = HttpContext.Current.Server.MapPath(BatchFilePath);
Take a look at Path.Combine
http://msdn.microsoft.com/en-us/library/fyy7a5kt.aspx
Is there a class or method in .NET 3.5 to get the last directory from a ftp Url?
I have in a string variable the ftp url like this and in this case I want to retrieve Directory2
ftp://user:password#server:port/Directory1/Directory2
In this case the root
ftp://user:password#server:port/
I was trying to find something like Path.GetDirectoryName(string) but I can't find.
I found that there is a way with Uri class and Segments.
The longest will be split by "/" and verify that is not the "/" from ftp://
There is another way already provided in .NET?
Thanks.
try this:
string ftp = "ftp://user:password#server:port/Directory1/Directory2";
string lastDir= ftp.Substring(ftp.LastIndexOf("/")+1);
lastDir will store the name of last dir
First:
string path = "ftp://user:password#server:port/Directory1/Directory2";
And then you can try with this:
string[] table = Path.GetDirectoryName(path).Split(Path.DirectorySeparatorChar);
And select last element of the table.
Ofc if you don't want the default separator you may define it in different way:
Path.GetDirectoryName(path).Split(new[] {"cool separator"}, StringSplitOptions.None);
I have xml files that contain href file paths to images (e.g. "....\images\image.jpg"). The hrefs contain relative paths. Now, I need to extract the hrefs to the images and turn them into absolute paths in the file system.
I know about the GetFullPath method, but I tried it and it only seems to work from the CurrentDirectory set, which appears to be C: so I don't see how I could use that. And still, I have the absolute path of the file containing the hrefs, and the href relative paths, so since it is a simple task for me to count back the number of "....\" parts based on the absolute path of the containing file, it seems there must be a way to do this programmatically as well.
I'm hoping there's some simple method I just don't know about! Any ideas?
string exactPath = Path.GetFullPath(yourRelativePath);
works
Assuming you know the real directory the XML file lives in use Path.Combine, e.g.
var absolute_path = Path.Combine(directoryXmlLivesIn, "..\images\image.jpg");
If you want to get back the full path with any ..'s collapsed then you can use:
Path.GetFullPath((new Uri(absolute_path)).LocalPath);
This worked.
var s = Path.Combine(#"C:\some\location", #"..\other\file.txt");
s = Path.GetFullPath(s);
It`s best way for convert the Relative path to the absolute path!
string absolutePath = System.IO.Path.GetFullPath(relativePath);
You can use Path.Combine with the "base" path, then GetFullPath on the results.
string absPathContainingHrefs = GetAbsolutePath(); // Get the "base" path
string fullPath = Path.Combine(absPathContainingHrefs, #"..\..\images\image.jpg");
fullPath = Path.GetFullPath(fullPath); // Will turn the above into a proper abs path
Have you tried Server.MapPath method. Here is an example
string relative_path = "/Content/img/Upload/Reports/59/44A0446_59-1.jpg";
string absolute_path = Server.MapPath(relative_path);
//will be c:\users\.....\Content\img\Upload\Reports\59\44A0446_59-1.jpg
This worked for me.
//used in an ASP.NET MVC app
private const string BatchFilePath = "/MyBatchFileDirectory/Mybatchfiles.bat";
var batchFile = HttpContext.Current.Server.MapPath(BatchFilePath);
Take a look at Path.Combine
http://msdn.microsoft.com/en-us/library/fyy7a5kt.aspx
I have C# code that is trying to get the LocalPath for a executing assembly using the following line of code:
Uri uri = new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath;
This piece of code performs fine for all the variety of paths. It started to fail giving the right AbsolutePath and Localpath because the executing assembly path contained a # in it.
Assembly.GetExecutingAssembly().CodeBase gives "C:\c#\ExcelAddin1.1.0\GSRExcelPlugin\bin\Debug\Common.dll"
But new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath gives "C:\c" while it should have given "C:\c#\ExcelAddin1.1.0\GSRExcelPlugin\bin\Debug\".
Is there something that I need to handle or is there something wrong the way Uri class is used?
If this is a .net framework issue, how should I report this issue to Microsoft?
System.IO.FileInfo logger = new System.IO.FileInfo(Path.Combine(Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath), "settings.config"));
Using EscapedCodeBase instead of CodeBase solves the problem. I dint realize that this was already handled until I stumbled on it.:)
The Uri class is behaving as expected. # is not a valid part of a url. Imo this is an issue with CodeBase property. It returns a string unescaping special characters rendering it pretty much unreliable. Your options are:
1) Always use EscapedCodeBase property instead.
var uri = new Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath;
2) Or if you really have to deal with CodeBase property (if that is the only option), get the escaped part yourself.
var x = new Uri(Assembly.GetExecutingAssembly().CodeBase);
var uri = x.LocalPath + Uri.UnescapeDataString(x.Fragment).Replace('/', '\\');
3) For file location of the loaded assembly, the easiest is to use Location property.
var uri = Assembly.GetExecutingAssembly().Location;
The issue has been reported several times, but MS insists that is how CodeBase is designed here, here, here and here.
I assume The URI functions are stripping away everything after the sharp # because it thinks its an anchor.
URI are designed for identifying resources on the internet, so your # character would be an illegal character in the middle of any URI.
Take even this question for example, the Title is
System.Uri fails to give correct AbsolutePath and LocalPath if the Path contains “#”
but the end of the URL has the "#" stripped away
system-uri-fails-to-give-correct-absolutepath-and-localpath-if-the-path-contains
Why do you need to convert it into a URI anyway. The only difference between these 3 console.writelines is the fact that the first two are prefixed with File:///
Console.WriteLine(Assembly.GetExecutingAssembly().CodeBase); // 1
Uri uri = new Uri(Assembly.GetExecutingAssembly().CodeBase);
Console.WriteLine(uri); // 2
Console.WriteLine(uri.LocalPath); // 3