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.
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 fairly unique situation. Did a lot of searching and most of what I'm seeing terminates at finding a particular file, using wildcards ("*.txt") as an example. What I need to do is move a file between paths, the first one having a changing subdirectory. I am downloading a .zip, extracting it, and moving a file who's name never changes. Its parent directory does change in name, based on a datestamp.
//original extracted folder
string path = #"C:\IP-Test_20140715\File.csv";
//where to move
string path2 = #"C:\File.csv";
File.csv will never change, yet IP-Test_20140715 will change based on the date (whatever the extracted folder is called), everything after the underscore will be different going forward.
If not possible to have wildcards in directories, is it possible to force the name of the extracted directory in c# using ZipFile.ExtractToDirectory?
Use:
Directory.EnumerateFiles (#"C:\IP-Test_20140715", "*.txt")
to enumerate over the different files.
Thus:
foreach(var subdir in Directory.EnumerateDirectories (#"C:\", "IP-Test_*")) {
foreach(var file in Directory.EnumerateFiles (subdir, "*.cvs")) {
File.Move(file,Path.Combine(#"C:\",Path.GetFileName(file)));
}
}
On the other hand, I don't see why you want to use C# for this? A simple bash script could do the trick way easier...
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.
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);