Downloading bin folder's dll file of ASP.Net App - asp.net

I just want to know , Is this possible to download my application's dll file from production server's bin directory...

Not via HTTP, if that's what you mean. You don't generally want to make that file available in that way.
If for some reason you want to make it available, like in some kind of code sharing scenario, I would code up a page that streams it out directly:
var fullQualifiedPathToDll = Server.MapPath("/") + "/bin/mydll.dll";
var myFileStream = new FileStream(fullQualifiedPathToDll, FileMode.Open);
var fileSize = myFileStream.Length;
var buffer = new byte[(int)FileSize];
myFileStream.Read(buffer, 0, (int)FileSize);
myFileStream.Close();
Response.BinaryWrite(buffer);
Be VERY sure that this is what you want to do when you're doing it. This is adapted from a sample found here.

Under normal circumstances, no. ASP.NET blocks requests to the bin directory.

Related

What's the recommended way to load an internal file on the web site?

We've got a certain image in the \Images folder of our web site. We need to include that image in an OpenXml file we're generating internally, and for that we're using the following snippet:
var logo = Server.MapPath(#"~\Images\logo-new.png");
var imagePart = mainPart.AddImagePart(ImagePartType.Png); // mainPart is of type MainDocumentPart
using (var stream = new FileStream(logo, FileMode.Open))
{
imagePart.FeedData(stream);
}
Then later imagePart is used for embedding in the document.
This code works fine in development, but in deployment we're getting a System.UnauthorizedAccessException when we try to open the file for streaming.
Clearly there is an access permission problem, since Server.MapPath() is converting the web path to an absolute path on the server drive, and the IIS user doesn't have rights to that. We might be able to get around it by granting access to everyone, but something tells me that this is not the textbook way of doing it. Surely there must be a way of accessing this file that doesn't require us to start futzing with access permissions to the web deployment folder?
Solved, by including the file as a resource rather than by trying to access it through the file system:
var logo = Resources.logo_new;
var imagePart = mainPart.AddImagePart(ImagePartType.Png);
using (var stream = logo.ToStream(ImageFormat.Png))
{
imagePart.FeedData(stream);
}
Hat tip to this answer for the .ToStream() extension.

Download and run file in client machine using asp.net

I'm trying to download and run a file to the client machine. The client is aware of that.
It's a ttkgp file that's dynamicly generated.
I've tried using Processs.Start() that worked fine on my local machine (first saved the file to C:\ then lunched it), but it's not working from the server. It's not my server but a hosted one. They are trying to help but no luck so far.
I've seen this code:
public void ProcessRequest(HttpContext context)
{
string fileName = context.Request.QueryString["filename"];
FileInfo fi = new FileInfo(fileName);
context.Response.ContentType = "application/x-rar-compressed";
context.Response.AppendHeader("Content-Disposition",
string.Format("attachment; filename=download{0}", fi.Name));
context.Response.WriteFile(fileName);
context.Response.End();
}
But since I dont know what's "HttpContext context" is, I've no idea if it works.
Is it some server previlges need to be changed? or simply this code will do the trick?
Thank you
UPDATE (24.6.12): I'm nearly finished with the problem, all I need now is to know how to open an html page in a new tab / window and close it second later. Once I'm done, I'll post back here all the process, I'm sure it'll help other people.
UPDATE (26.6.12):
Here's what I've got:
The goal is to download an TTKGP file from asp.net webiste to local user machine and run it.
Step 1: generate the file with code behaind (c#) on the server (V)
Step 2: copy the file or it's content to user machine (X)
Step 3: run the file using JS (V)
Here's the thing: I CAN copy from a text file on the server to a text file on the user machine, but not from TTKGP files. It's strange because this are just text files just a different extantion.
The code for copying text files:
enter code here
function copyremotetxt() // works
{
// copy the txt file
var fso = new ActiveXObject("Scripting.FileSystemObject");
var newfile = fso.CopyFile("remote.txt", "C:\\Users\\***\\local.txt");
}
Perhaps I can change the file type on the user machine?
Notice 1: I know that's a security issue, the site is just for known user not the open public
Notice 2: I know there are better ways to get the task done, but there are strict limitaions on many things
Thanks for those how can help!!
This code will do the trick. It will prompt the client to download and save the file on his computer at the location he decides. What happens next with this file is the client's decision, not yours. He might simply close the Save As dialog, interrupt the download, delete the file, ... It's up to him.
Another remark: this code is extremely dangerous because it allows the client to pass any filename he wants as query string parameter and download it. So he could read absolutely all files on the server which is probably not something that you want to happen.
Ok, this need a different aproach.
I'll try using JavaScript do read the file on the server, rewrite it in the user machine and activate it. Any clues would be grate! For a start, how to I read file in JS? I'm new to it.

asp.net File.OpenText can't read uploaded file

I have an ASP.NET MVC application on Windows Server 2008.
I need to upload a file, save it to an archive folder under the App_Data folder, then open and read from it. I can do this on my local machine but can't on the test server. I suspect it is a permissions issue but the permissions appear to be in place. The relevant C# code:
HttpPostedFileBase hpf = Request.Files[0];
var fileLength = hpf.ContentLength;
if (fileLength != 0)
{
var archiveFolder = Server.MapPath("~/" + folder);
var archiveFile = "import_" + DateTime.Now.ToString("yyyyMMdd_HHmm") + ".txt";
var archivePath = Path.Combine(archiveFolder, archiveFile);
hpf.SaveAs(archivePath);
}
The above code saves the file fine.
StreamReader sr = File.OpenText(archivePath);
The above line throws an error:
Could not find file 'c:\windows\system32\inetsrv\usb01312012.txt'.
So although "archivePath" is a path to a saved file under App_Data, ASP.NET looks to the SYSTEM folder for the file.
I have given every permission except Full Control to IIS_USRS on the entire website. Why can't I access the file?
Make sure your site runs correct version of asp.net (2.0 or 4.0).
RESOLVED: As of the next day, this now works! I guess IIS had to recycle? Thanks for your comments!

Programmatically retrieving IIS log file location in an ASP.NET application

I'm trying to determine the location of the IIS log file location of my ASP.NET application. I tried WMI, but wasn't able to find it. Any suggestions?
Note: I want to be able to retrieve the location programmatically; inside my application for further use.
Edit: Here's my code: This works but does not give me the actual physical directory location of the logs. So this is basically useless.
ManagementPath p2=new ManagementPath("IIsLogModule.Name='logging/Microsoft IIS Log File Format'");
ManagementObject log = new ManagementObject(scope, p2, objectGet);
log.Get();
logPath.Text = log["__PATH"].ToString();
On IIS7 you could use Microsoft.Web.Administration assembly, Site class has a property named LogFile, you can get various info about log file for site, for example log file directory can be obtained with this code:
ServerManager manager = new ServerManager();
Site mySite = manager.Sites["SiteName"];
Response.Write("Log file directory : " + mySite.LogFile.Directory + "\\W3svc" + mySite.Id.ToString());
I don't like very much that hardcoded part with directory prefix for site, but didn't find any other better way
You should able to use ADSI (or WMI) to do that - browse IIS metabase and look for 'LogFileDirectory' property for the web site node. For example,
var root = new DirectoryEntry(#"IIS://localhost/W3SVC");
var sites = root.Children;
foreach(DirectoryEntry site in sites) {
var name = site.Properties["ServerComment"][0];
var logFile = site.Properties["LogFileDirectory"][0];
}
Disclaimer: Untested code
See this powershell example using similar idea.

How to load WSDL from file

I am trying to save some bandwidth and include wsdl file in my flex/air application. Which url format should I use in order to load that file instead of the remote one.
I am using loadWSDL() method.
EDIT:
wsdl file needs to be part of the application. I know I can use file://some/path for local files, but don't know how to load file which is inside application itself.
If the file is local, just use the file URI scheme:
file://host/path/file.wsdl
If this doesn't work, check if the security sandbox features are blocking it.
In AIR apps, in order to access files in the application's temporary storage directory or the application's own directory, you need to use special app: or app-storage: URL schemes, though.
Like dirkgently said, you can always embed the file into the application, but as far as I know, you then won't be able to modify it afterwards in a persistent manner since it's not just a file in the filesystem. Probably the best option for you is to embed this file and if you later need to update it, have the app save an updated version into the File.applicationStorageDirectory (which you would then always check first before using the default embedded version.) Although I have no idea if using embedded XML files with the WebService classes is even possible.
See this article for info on how to embed external XML files into your app. This is how I've done it:
// note: common sense says that the mimeType should be "text/xml" here but
// it doesn't work -- this does, though. who knows why.
[Embed(source="File.xml", mimeType="application/octet-stream")]
private const _fileXMLClass:Class;
private var _fileXML:XML = XML(new _fileXMLClass());
wsdl file needs to be part of the application.
Have you tried embedding it inside the Flex/AIR project as a resource? Read this. For example, you can load static images shipped with your app by specifying the source as:
source="#Embed(source='relativeOrAbsolutePath')"
Uf, this was ugly, so I'm answering for the reference. Thanks for insights to hasseg and dirkgently
Here is the code.
First, declare the variables:
[Embed(source="/ws/wsdl/LoginService.wsdl",
mimeType="application/octet-stream")]
private const _fileXMLClass:Class;
private var _fileXML:XML = XML(new _fileXMLClass());
Then, loading wsdl:
var file : File = dir.resolvePath(name + ".xml");
var stream : FileStream = new FileStream();
stream.open(file, FileMode.WRITE);
stream.writeUTFBytes(getWsdl().toXMLString());
stream.close();
loadWSDL(file.url);
If someone have an idea to make this less ugly, please let me know.
EDIT: I just noticed edited answer, so instead of this code it was enough to use just:
loadWSDL('app:///path/to/my/file.wsdl');
I use below code in flash builder air mobile app and it works, may help some else. I get file contents from a web service using url loader and wirte it down to a xml file in document directory of my air app.
var url:URLRequest = new URLRequest(Globals.deviceSettings.endpoint);
loader.load(url);
loader.addEventListener(Event.COMPLETE, loaderComplete);
get the status of web service, if it is 200 then available and heads up.
loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, ldrStatus);
and in the eventlistener
function loaderComplete(e:Event):void
{
var f:File= File.documentsDirectory.resolvePath("source/category.xml");
var _xml:XML = new XML(loader.data);
var fs:FileStream = new FileStream();
fs.open(f, FileMode.WRITE);
fs.writeUTFBytes(_xml.toXMLString());
fs.close();
popup.close(true);
var popup:MyPopupComponent = new MyPopupComponent();
popup.show("Successfully updated from the server",this);
popup.close();
}
you can use file.documentdirectory or applicaiton or your choice directory as per your need please keep in mind that some of the paths are read only for security. if you want to write back to those files you wont be able but just for reading purposes it is a good idea to place the files there.

Resources