I want to get the path of the temp folder (C:\Users\user\AppData\Local\Temp).
GetEnvironmentVariable("TEMP") works fine, but I get the path with tilde:
C:\Users\STANHE~1\AppData\Local\Temp\
.. and I need the path without the tilde:
C:\Users\StanHerrmann\AppData\Local\Temp\
You can use Path.GetFullPath to expand it:
If you pass in a short file name, it is expanded to a long file name.
But... it would be better to just use Path.GetTempPath for this, so you don't have to rely on environment variables to be correct.
Related
There is a little confusion in my mind about server.mappath
which is correct and what's the difference betwwen these two
FileUpload1.saveAs(Server.MapPath("~/User/images/")+"ankush.jpg"));
FileUpload1.saveAs(Server.MapPath("~/User/images")+"ankush.jpg"));
The correct way of using MapPath() would be:
FileUpload1.saveAs(Server.MapPath("~/User/images/ankush.jpg"));
or if you insist:
FileUpload1.saveAs(Path.Combine(Server.MapPath("~/User/images"),"ankush.jpg")));
MapPath() doesn't append a trailing backslash to the mapped path because it has no way of knowing if the path is a directory or a file (it doesn't check if the given path actually exists)
I would advise you to use this way
FileUpload1.saveAs(Server.MapPath("~/User/images/ankush.jpg"));
Reason : because if you already know the path then why break down the filename separately
If the filename was getting passed by parameter then you could do
FileUpload1.saveAs(Server.MapPath(String.Format("~/User/images/{0}", fileName)));
I have a System.Win32.RegistryKey object that points to, for example, "HKCU\Software\Test". The .Name property is populated with the absolute path. Is there a way to get only the current (relative) key name?
In the example above, I am looking for just the "Test" part of the path. I am looking for the Registry equivelent to System.IO.Path.GetDirectoryName without having to parse the path manually.
You can just call System.IO.Path.GetDirectoryName.
The Path.* functions (except for GetFullPath()) are purely string manipulation functions and work fine even with non-filesystem paths.
The question asks for the relative value (just Test) not the absolute full path. The GetDirectoryName method returns the absolute path.
The method you want to use is:
System.IO.Path.GetFileNameWithoutExtension(subkey.Name);
See this link
You can simply use System.IO.Path.GetDirectoryName(string) which works just fine with registry paths.
I am using Directory.Enumeratefiles to get the path some images stored in a particular directory.
The directory.enumeratefiles method returns the path as "D:\Gallery\Test\1.jpg"
Anyway when I bind it to a image in a repeater the path turns into "D:\Gallery\Test\1.jpg", but the image doesnt display in the page.
I just want to know whether the path can be converted to tilde (~) when binding.
You'll have to map the real paths to virtual paths yourself; basically the reverse of Server.MapPath.
The easiest way to do this is to run something like this:
var root = Server.MapPath("~");
var relative = realPath.Replace(root, "~").Replace("\\", "/");
I have the code
string xsltPath = System.Web.HttpContext.Current.Server.MapPath(#"App_Data") + "\\" + TransformFileName
It returns
C:\inetpub\wwwroot\websiteName\SERVICENAME\App_Data\FileName.xsl
Why am I getting the path to the ServiceController, SERVICENAME? I want the path to App_Data which is in
C:\inetpub\wwwroot\websiteName\App_Data\FileName.xsl
You need to specify that you want to start from the virtual root:
string xsltPath = Path.Combine(System.Web.HttpContext.Current.Server.MapPath(#"~/App_Data"), TransformFileName);
Additionally, it's better practice to use Path.Combine to combine paths rather than concatenate strings. Path.Combine will make sure you won't end up in a situation with double-path separators.
EDIT:
Can you define "absolute" and "relative" paths and how they compare to "physical" and "virtual" paths?
MSDN has a good explanation on relative, physical, and virtual paths. Take a look there.
The answers given so far are what you are looking for, but I think, in your particular case, what you actual need is this:
AppDomain.CurrentDomain.GetData("DataDirectory").ToString()
This will still return the file path to the App_Data directory if that directory name changes in future versions of MVC or ASP.NET.
Try doing like this (#"~/App_Data"). ~/ represents the root directory.
I'm something of a n00b at C# and I'm having trouble finding an answer to this, so if it's already been answered somewhere feel free to laugh at me (provided you also share the solution). :)
I'm reading an XML file into a GUI form, where certain elements are paths to files that are entered into TextBox objects. I'm looping through the controls on the form, and for each file path in each TextBox (lol there's like 20 of them on this form), I want to use File.Exists() to ensure it's a valid file.
The problem with this is that the file path can potentially contain spaces, and can potentially be valid; however File.Exists() is telling me it's invalid, based entirely on the spaces. Obviously I can't hard-code them and do something like
if (File.Exists(#"c:\Path To Stuff"))
and I tried surrounding the path with ", like
if (File.Exists("\"" + contentsOfTextBox + "\""))
but that didn't make a difference. Is there some way to do this? Can I escape the spaces somehow?
Thank you for your time. :)
File.Exists works just fine with spaces. There is something else giving you a problem I'll wager.
Make sure your XML reader isn't failing to read the filename (parts of XML do not allow spaces and some readers will throw an exception if they encounter one).
#"c:\Path To Stuff"
The above could be a directory not a file!
Hence you would want to use Directory.Exists!
#"c:\Path To Stuff\file.txt"
If you did have a file on the end of the path then you would use File.Exists!
As the answer said, File.Exists works with spaces, if you are checking for existence of a Directory however, you should be using Directory.Exists
What is the exact error that you get when File.Exists says it is invalid?
I suspect that you are passing a path to a directory and not a file, which will return false. If so, to check the presence of a directory, use Directory.Exists.
To echo Ron Warholic: make sure the process has permissions over the target folder. I just ran into the same "bug" and it turned out to be a permissions issue.
Did you remember to replace \ with \\ ?
You need to use youtStringValue.Trim() to remove spaces leading/trailing, and Replace to remove spaces in the string you do not want.
Also, rather use System.IO.Path.Combine rather to combine these strings.
You can use # on string variables:
string aPath = "c:\Path To Stuff\text.txt";
File.Exists(#aPath);
That should solve any escape character problems because I don't think this really looks like the spaces being the problem.
hi this is not difficult if you can convert the name of the path to a string array then go through one by one and remove the spaces
once that is done just write() to the screen where you have the files, if it is xml then your xmlmapper will suffice
file.exists() should only be used in certain circumstances if you know that it does exist but not when there can be space chars or any other possible user input