Get Temp ASP.NET folder for application - c#

I'm trying to get a specific application base ASP.NET Temp folder. I know we can use HttpRuntime.CodegenDir property to get the actual temp asp folder that the application is writing to. Is there any way I can get location to the base folder for that application in C#? Example:
<server>\c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\<AppName>

For any other who stumble on this question,
I couldnt find any direct way to get it. the way i resolved this is to get executing assembly location using below line
Assembly.GetExecutingAssembly().Location;
And then get to the parent folder which is the application name recursively using
var dir = new DirectoryInfo(path);
while (dir.Parent.Name != folderName)
{
dir = dir.Parent;
}
return dir.Parent.FullName;
which will give me the required path im looking for.

Related

How to get the program's current folder name and extract the zip file to the current folder in C#? [duplicate]

This question already has answers here:
Get current folder path
(13 answers)
Closed 2 years ago.
I have a Winforms Auto Updater.
If I open the program it will get the file download link from a raw text which contains the .zip file download link to the program from the web.
But if the person made a folder and put the program in the folder I want the program to get the folder name the program is in and extract the zip file to it.
Here's my code:
private void StartDownload()
{
WebClient webClient = new WebClient();
string path = "./Sympathy";
string address1 = "https://ghostbin.co/paste/cjx7j/raw";
string address2 = webClient.DownloadString(address1);
this.progressBar1.Value = 100;
string str = "./Sympathy.zip";
if (System.IO.File.Exists(str))
{
System.IO.File.Delete(str);
}
else
{
webClient.DownloadFile(address2, str);
ZipFile.ExtractToDirectory(str, Directory.GetCurrentDirectory(Directory).Name);
System.IO.File.Delete(str);
}
}
But I'm having an error on the (Directory) part, How do I fix it?
To get the path to your executable use System.Reflection.Assembly.GetEntryAssembly().Location;, to get the directory use Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
I see that your code is flawed. Use this instead, to do everything :
string archivePath = "Sympathy.zip";
using (var client = new WebClient())
{
client.DownloadFile(client.DownloadString("https://ghostbin.co/paste/cjx7j/raw"), archivePath);
ZipFile.ExtractToDirectory(archivePath, Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location));
File.Delete(archivePath);
}
It works. It downloads a bunch of unknown files.
What is the error that you receive? That will help with the issue that you have.
You mention that you get an error on the (Directory) part of your code, there are some things that I see on this line:
The method Directory.GetCurrentDirectory doesn't expect an input;
You can't pass a type as a parameter;
The call Directory.GetCurrentDirectory() returns a string, you can't call the property Name on this.
Be aware that the result of this call is not always where your application is residing. If you check the properties of a hyperlink, you can set the Target' and the Start in. The Target is your program and the Start in is the current directory that is provided to your application. Instead of creating a hyperlink, you can also open a command prompt (defaults to your home directory) and type the full path to your application, this will cause your application to open and receive as current directory the path where your command prompt was pointing to (in case of the default, your home directory). The answer in post Get current folder path goes a bit deeper in detail about this.
With this Line You Can Get The Current Directory of Your Application.
Application.StartupPath;
it gives you the Folder of your .exe running app.

Get Base Directory of one project from another project's class

I have two asp.net projects in one solution. One is WebForms and the another is a WebApi project.
I am trying to save an image in the root folder of the WebForms using a method in the WebApi. Unfortunately, I am unable to get the BaseDirectory of the WebForms from the WebApi method.
This is what I have tried:
//this returns me only the root folder of the API i.e; D:\Projects\MyApp\API\)
string basePath = System.AppDomain.CurrentDomain.BaseDirectory;
Is it possible to do what I am trying?
I guess you can navigate to you related app if it destination meet some folder tree position requirement using this:
string relativePath = "..\\Web\\";
string basePath = System.AppDomain.CurrentDomain.BaseDirectory;
string webPath = Path.GetFullPath(baseDirectory + relativePath);

How to get the folder path using folder name c#

I'm using C# to get the exact path of the specific folder in windows system by giving the folder name. Is their any way to get the folder path by giving the folder name, where the folder name will be unique.
Update:
Folder is created at run time with current time as the name. This
process is done by the application. Here i know the folder name but i
didn't know path, because path is selected by the user during
installation and installation is done before very long time.
That changes the question considerably. Why not use the application to tell you where it lives:
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.startuppath.aspx
I had a similar idea ages ago and wrote about it as a Code Project Tip:
http://www.codeproject.com/Tips/132804/Open-folders-using-a-Run-Command
Otherwise you would need to index every folder on the PC and make them unique names and look up the full path that way.
The other suggestion I have is using LogParser as the Most efficient way to find all exe files on disk using C#? Its a free Microsoft product but I'm not sure about re-dist permissions, I had to include it in my package separately last time I used it. It full on flys, faster than a speeding train!
I found a Log Parser example that finds folders, you could try it out and adapt it if its useful:
SELECT TOP 1 * FROM C:\TFS\Project\*.* WHERE INDEX_OF(Path, 'Database') > 0
The good folks over at http://visuallogparser.codeplex.com/ have
provided us with the source code.
Open the VisualLogParser solution in VS2010, ignore the prompt about debugging, after the solution loads, F5, set the combo-box to FS (FileSystem), paste in this query and press go.
You could probably use something like this, but it'll be rather slow, depending on how many folders needed to be looked through.
Use it like FindFullPath(rootFolder, folderNameToLookFor)
public static string FindFullPath(string path, string folderName)
{
if (string.IsNullOrWhiteSpace(folderName) || !Directory.Exists(path))
{
return null;
}
var di = new DirectoryInfo(path);
return findFullPath(di, folderName);
}
private static string findFullPath(DirectoryInfo directoryInfo, string folderName)
{
if (folderName.Equals(directoryInfo.Name, StringComparison.InvariantCultureIgnoreCase))
{
return directoryInfo.FullName;
}
try
{
var subDirs = directoryInfo.GetDirectories();
return subDirs.Select(subDir => findFullPath(subDir, folderName)).FirstOrDefault(fullPath => fullPath != null);
}
catch
{
// DirectoryNotFound, Security, UnauthorizedAccess
return null;
}
}
See following link
string dirName = new DirectoryInfo(#"c:\projects\roott\wsdlproj\devlop\beta2\text").Name;

Getting directory from PageFiles folder of EPiServer

I'm having some thoughts about how the EPiServer PageFiles system work. When I get a global file it's simple to get the correct directory, I just write:
var dir = VirtualPathHandler.Instance.GetDirectory("~/Global/myfolder/", true)
But when I want to get files from the PageFiles folder it gets tricky for me. The path in the EPi backoffice upload says it is PageFiles/myfolder, but this doesn't return any folders
var dir = VirtualPathHandler.Instance.GetDirectory("~/PageFiles/myfolder/", true)
Now I see that files are saved different for each page, one page has the path /PageFiles/361/myfolder/ and another page has the path /PageFiles/65/myfolder/. All I want to do is be able to write
var dir = VirtualPathHandler.Instance.GetDirectory("~/PageFiles/myfolder/", true)
but to get any folder I have to write (for instance)
var dir = VirtualPathHandler.Instance.GetDirectory("~/PageFiles/65/myfolder/", true)
What is the correct way to get a PageFiles folder so I can access all files in it?
BTW, this is EPiServer 6.0
Try the GetPageDirectory method on your PageData object
http://sdk.episerver.com/library/cms6/html/M_EPiServer_Core_PageData_GetPageDirectory.htm

ASP.NET MVC Get file from virtual path

For various reasons, in development I occasionally want to intercept a request for, say, ~/MyStyle.css
What I want to do is make the following snippet work:
string absFile = VirtualPathUtility.ToAbsolute(file);
return System.IO.File.ReadAllText(absFile);
This absolute path is absolute for the webserver though, it's not going to map to "C:\whatever". Is there an equivalent method to go to the file system? (Or a ReadFromVirtualPath etc.?)
Use Server.MapPath() to get the file system path for a requested application path.
string absFile = Server.MapPath(file);
or
string absFile = HttpContext.Current.Server.MapPath(file);
You can also use the OpenFile method on VirtualPathProvider to get a Stream pointing at your file
var stream = HostingEnvironment.VirtualPathProvider.OpenFile(file);
var text = new StreamReader(stream).ReadToEnd();
Generally this approach is preferable since you can now, at a later point implement a VirtualPathProvider where, lets say all your css files where located in a database.

Categories

Resources