Run Batch File in Asp.net C# - asp.net

I am using asp.net C# 4.0
I have a batch which will open a text file.
Batch File query
ECHO OFF
start D:\accounts\request\08__processing\0377e792-4ca9-4550-b78c-de2bdf26611f.txt
ASp.net Code
System.Diagnostics.Process.Start("D:\\bacthFile.bat");
when i double click on the batch file its opening the text file.But when run above code its not opening any notepad.
it is not showing any exception also.
Please Help
Thanks

This is the wrong way to read a text file from ASP.NET, you need to use System.IO and put files you need to read somewhere where ASP.NET can get at them, eg.. App_data. That said, here is how to do it the wrong way: IIS runs on an invisible virtual window--it's a service. There is a way to get some services to display the UI, I forget how. In any case, you'd have to RDP onto the server, to see such a window after starting the service and goosing it into displaying a UI (this trick might not even work for IIS).
Next, as commenters noted, on IIS, you'll have one set of credentials different from your own (depends on what version of IIS)-- in any case, it will have restricted access and be somewhat sandboxed. If you set up impersonation and windows authentication in the web.config, sometimes you can get your request to run with your credentials.
Next, if the web host runs in medium trust, you might not be able to launch arbitrary apps from the asp.net appdomain.
Finally, the only way this could ever work is if your app is always running locally with casinni (the visual studio development server)-- but in that case, you could simplify things a lot by using a console app instead of asp.net unless you really need the HTML templating, say for output.

Related

ASP.NET app cannot see files

I have an ASP.NET web app (VS 2017 Framework 4.5) that works fine in development. When I deploy to the web server and try to reference a file like this:
PdfBitmap tiffImage = new PdfBitmap(item.TIFPageLocation);
It returns a "Can't find file error". I even set up an if File.Exists() test and had the same result.
The file is a reference to another server like this: \\myserver\myvolume1\00\12\7A\00127A90.TIF.
When I run this path on the web server in WinExplorer it finds and open the TIF image in the default viewer. However the web app cannot see it for some reason.
This is likely some permissions issue but I'm not sure where.
Any ideas?
Thanks
Carl
Yes, this is likely a permissions issue. Your web app will probably be running under an account that has limited rights (almost certainly without the rights to access anything on the domain).
You either need to:
a) elevate their rights (be very careful with this)
b) run your site under a new user context created specifically for this site with rights to reach the other server
c) put your images somewhere easier to access.
Check your folder permission it should not have read access cross check that it's worked for me
It was the account the app pool identity was associated with. That need to be set properly.

ASP.NET Cannot launch Excel's file on server

i'm trying to open launch Excel specifing an existing file to open. This is my code:
Protected Sub fileManager_SelectedFileOpened(source As Object, e As FileManagerFileOpenedEventArgs)
Try
Dim FullName As String = e.File.FullName
System.Diagnostics.Process.Start(FullName)
dvFileError.Visible = False
Catch ex As Exception
dvFileError.Visible = True
lblFileError.Text = ex.Message
End Try
End Sub
In the e.File.FullName i can found the file's name with its path, so when i launch it as process it should open the Excel's file.
So this code works on local, but when i upload it on a web server it not works. It say "An error occurred in sending the command to the application".
I've created a .txt file to test if my application can access to the folder an i take this the "Access is denied" error.
It is strange because if i open the same Excel's file using a ASPxSpreadSheet Control of DevExpress, the file is opened.
Any solutions?
Your web service can't just open an executable on the client in normal circumstances. There's a few exceptions:
Your web server has admin privileges on the client. This typically only happens in a company LAN scenario
Your web service is the same machine as the client.
You have some sort of software running on the client that can launch the program as necessary. This requires technology outside of the web HTML/CSS/JS sandbox of the browser.
You were operating under #2. But as soon as you switch to remote clients that isn't going to work. You can't use System.Diagnostics.Process to start applications on a remote client. And you can't directly access files on the remote clients machines. Your VB.NET code is running on the server, not on the client. Only your HTML/CSS/JS runs on the client, and that's in the limited sandbox of the browser.
The general pattern to accomplish what you want is just offer a file for the user to download by writing the file to an HTTP response. The user can then open the file and it will launch their default program for editing that filetype.
Before I start, I can tell you that this is one of the worst things that you can do - is to run Excel application on the web server.
If you need to read or save Excel file(s) use Microsoft.Ace.OleDb.xx (xx-version). It will allow you to work with Excel as if it was regular DB. Moreover, if in connection string you give file name of the file that doesn't exist, it will create it.
Remember, you're on the server. And your application runs in application pool. The application pool runs under some user account, which may not have permission to open any user interface.
I remember, we had an issue with Windows service and Excel automation. Excel needs the account, which runs it to be literally logged on into machine. In other words, you need to use the credentials of the account that runs the app pool to logon into this machine. Only then Excel automation will work.
In any case. When you do Excel automation first thing you need to do is to open Excel once to get rid of all the dialogs it displays from beginning. And until you click them off, each instance will keep on showing them even if you don't see them or instance.
And last couple words - you need to use some interop assemblies, not just process.start. It will take care of some things. But Microsoft told us - they do not support or recommend Excel automation, although, admit - it is possible. Returning to paragraph #2 - to write or read data do that, not what you do. You can do #2 in IIS process. But don't do automation in process - very bad idea.

Crystal Reports Viewer: Failed to Open the Connection error

Currently I'm working on a piece of code that involves using the Crystal Reports Viewer to export .RPT files to PDF. My problem is that the code works on literally everything except the website I'm planning to eventually put it on.
When placed on the website it throws back the error "Failed to open the connection." which I believe is connected to the SQL Server connection that's formed in order to read data for the report itself.
The code itself is solid and works when used in a separate console application I built to test it whether said code is run on my local machine or on the server itself. It even runs when I test the website in Visual Studio's virtual host.
I've narrowed it down to being some sort of permissions issue, but I am not knowledgeable enough in the various permissions settings to figure out which ones specifically apply to this situation. It's worth mentioning that other pages on the website are directly connecting to the same database, but they are doing so directly through Linq instead of going through the Crystal Report Viewer.
So does anybody know which settings I need to modify in order to fix the connection error.
An added note: One of the other suggestions is to make sure there's an ODBC DNS in place that matches the one being used by the code. I have created such a DNS already.
Application Pool dictates how your application runs in many ways. You can probably configure the web.config to run under the credentials, or access level of your choice.
Solution for people who didn't read the comments:
Change the identity under the advanced setting's of your application pool to the desired level of access.
Glad I could help.- J

Deploy a website from Visual Studio to webserver

I'm using a blank VS2010 solution to manage a static website I maintain. I was going to use the ASP.NET website project, but that added a bunch of stuff the webserver wouldn't do. If I should still use that project, please let me know!
I have the code under source control and try to have the DEV region in source mirror the DEV webserver. I want to migrate my changes to the dev server for others to view, but I'm not sure of the best method to do this. If I use the Publish Website command in VS, it will delete the files on the server and copy all the files. The problem with this is that it takes waaayyy too long. Especially when I am on the VPN. I could manually copy the files, but that's a sloppy way to do it. And the server doesn't have FTP so that's not an option either. Is there some blatant method I am missing?
I thought about setting up a workspace with the server as the working folder. Then, whenever I wanted to migrate a change, I'd just do a "get latest" in that workspace and it would bring down any files that have changed. Does this sound like an okay method or is there a preferred method for this?
Have you tried the copy/website functionality
First of all, I recommend against using web site "projects" for anything. Use a Web Application Project instead.
Secondly, when you use MSDEPLOY from the Publish command, it synchronizes the target web site with the source - it will only deploy changed content.
Set up a continuous integration server (ex. CruiseControl.NET).
Create a new build project for each website you wish to deploy, initially configured for manual invocation.
Configure the build project to do a get latest and deploy.
Here are some possible implementations:
http://callicode.com/Homeltpagegt/tabid/38/EntryId/27/How-to-only-publish-the-runtime-files-of-an-asp-net-application-using-CruiseControl-net.aspx
http://confluence.public.thoughtworks.org/display/CCNET/Build+Publisher

Is it possible to perform Web.config transformations without publishing?

I am using VS2010, and I understand that when I publish a web application, the Web.config I am using is transformed based on the build settings.
i.e. When I choose Debug, it uses my development SQL server, and when I choose Release, it uses my production SQL server.
What I want to know is: Can I, using only the built in Visual Studio Development Server, select "Release" from the Configuration drop-down, and then the green "Continue" arrow (F5), and run using the transformed Web.config settings?
I would be quite happy having another file such as "generic.config" (or better: "Web.Generic.config"), which holds the master code if necessary.
I don't believe there's a simple way to do this. I would have thought it's unusual to want to run code locally against a production database, but maybe you're not in Enterprise Land which is probably what this functionality was designed for.
An alternative would be to publish your site to a server, and then use Remote Debugging to step into the code.
The plain web.config is your local and generic settings combined. The transforms tweak the generic data to match the environment you're deploying to.
There's a handy site if you just want to quickly test the results of a web.config transform: http://webconfigtransformationtester.apphb.com

Resources