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)));
Related
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.
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 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
What's the best way to get a string containing a folder name that I can be certain does not exist? That is, if I call DirectoryInfo.Exists for the given path, it should return false.
EDIT: The reason behind it is I am writing a test for an error checker, the error checker tests whether the path exists, so I wondered aloud on the best way to get a path that doesn't exist.
Name it after a GUID - just take out the illegal characters.
There isn't really any way to do precisely what you way you want to do. If you think about it, you see that even after the call to DirectoryInfo.Exists has returned false, some other program could have gone ahead and created the directory - this is a race condition.
The normal method to handle this is to create a new temporary directory - if the creation succeeds, then you know it hadn't existed before you created it.
Well, without creating the directory, all you can be sure of is that it didn't exist a few microseconds ago. Here's one approach that might be close enough:
string path = Path.GetTempFileName();
File.Delete(path);
Directory.CreateDirectory(path);
Note that this creates the directory to block against thread races (i.e. another process/thread stealing the directory from under you).
What I ended up using:
using System.IO;
string path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
(Also, it doesn't seem like you need to strip out chars from a guid - they generate legal filenames)
Well, one good bet will be to concatenate strings like the user name, today's date, and time down to the millisecond.
I'm curious though: Why would you want to do this? What should it be for?
Is this to create a temporary folder or something? I often use Guid.NewGuid to get a string to use for the folder name that you want to be sure does not already exist.
I think you can be close enough:
string directoryName = Guid.NewGuid.ToSrtring();
Since Guid's are fairly random you should not get 2 dirs the same.
Using a freshly generated GUID within a namespace that is also somewhat unique (for example, the name of your application/product) should get you what you want. For example, the following code is extremely unlikely to fail:
string ParentPath = System.IO.Path.Combine(Environment.GetEnvironmentVariable("TEMP"), "MyAppName");
string UniquePath = System.IO.Path.Combine(ParentPath, Guid.NewGuid().ToString());
System.IO.Directory.CreateDirectory(UniquePath);