C# Run my visual studio executable as administrator - c#

Hi i have created my visual studio program and if i run it as normal it works perfectly. But the problem comes in when i create a setup installation for this program. It successfully installs my program but i cannot get the program to run as administrator and thus my sql statements wont work. Any suggestions on how to fix this problem?

It is hard to pinpoint the fault but I have a feeling you have asked an XY problem question where running with and without elevated privileges (often wrongly called administrator). Making an installation program an installing probably has nothing to do with it. Maybe you are starting VS with elevated privileges without thinking about it?
Try to start the program directly and not through VS. Find the output folder (yourproject\bin\debug\yourprogram.exe) and run it. Does it work properly? Then it works.
Now try to start it with elevated privileges (admin) (ctrl-shift-enter or ctrl-shift-doubleclick). Does it work as it should? If it does, your problem has nothing to do with privileges.
So then you might have side effects by the installation. An installed file is in another directory. Do you have hard coded relative paths?

Related

Project exe build doesn't work

I have a problem in my visual studio or my computer that I can't figure out how to solve.
This is the situation:
I wrote a program and when I tried to run it from the .exe file, it didn't work. From debug, the program worked properly but if I tried to open a second instance of it, it didn't work.
In both cases, when I opened the second instance, or open the .exe file, the process of the program opened and didn't work (kept loading, or in the second debug instance case it got the VS stuck). When I tried to close the process I couldn't, even after closing visual studio task. The only way was to restart my PC, witch I did a lot.
After that, I tried to run the program on another computer and I found out it worked well.
I came back to my PC to try to solve the problem. I uninstalled VS13 and installed instead the VS15.
I opened a new WPF project. First thing i did was build it and open the .exe and it still didn't open!
This is my case guys. I would love to hear information on this problem, why it happend and how to solve it.
My PC is running Win 8.1 pro x64 operating system.
I found this that resembles my case:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/fbae61b4-47f1-4074-9261-505ebbd8d459/cant-run-visual-studio-2012-generated-exe-files?forum=vcgeneral
Thanks!
The soultion to my problem was simple, i can't believe i didn't thought of it before. thanks to the first comment.
Disabling avast anti-virus solve the problem, the exe of my new and my original program is working fine.
Now all i need to do is find a way to make it work with the anti-virus working.

Acess Denied to installed application on Windows 8

I've developed an application in Visual Studio 2010 but I used the VS 2012 to create the
Setup Installer. It's working perfect when running it on Visual Studio 2012, no errors at all.
Detail: I builded this when I used win7, now i'm using windows 8
But when I install the application and try to run it, when it tries to overwrite the app.config file it throws the exception. I already tried to give permission by RightClick - Security tab but didnt solve the problem. I found THIS thread that looks like my problem. But didnt worked... All I have on my app.config is the connections strings. What may it be?
UPDATE
I gave permissions to Users/PC1 in each file.cfg and to the whole folder that the files are in.
And now it seens that it worked, but what may I do to do not need do all of this to run the applpication ? I want to install without needing to give permission to the folders and files. (It's a win8 complications... -.- ).
It sounds like a permission issue, mostly with the project folder potentially.
You should try the following steps:
Run Visual Studio as an Administrator.
Verify that your User Account can Read / Write to your project folder.
Ensure that your Local Service, Network Service, or Local System
Off the top of my head those would be what I would try, more then likely Visual Studio isn't able to modify your app.config which would be tied to your Local Service account- The one that would be tied to your System.
Though the inner content isn't the problem, it is tied to your Directory Permissions which may be inherited to the sub-folders and sub-files.
Hopefully that helps.

Trying to run a powershell script in C#

I've got a PS script that adds firewall rules.
I'm using Process.Start("powershell.exe", "-File "+"\"C:\Program Files (x86)\blah\blah\add-FirewallRule.ps1\"");
This command works fine on my local machine, but in testing I've been running the program on a Windows Server 2008 R2, and the PS script won't run. It runs and works fine if I run it manually from the command line, but my code won't work. All the file paths are correct (i've already checked that and had a co worker check it). When I run the code the PS console doesn't even pop up.
Any suggestions?
Since you are in C#, I'd recommend using the PowerShell Invoke API rather than Process.Start. The API is easy to use and hopefully will give you a better indication of what is going wrong.
Turns out that I wasn't accounting for the automatic redirect caused by running a 32-bit app on a 64-bit system. So all I had to do was go in and set the appropriate execution policy for the 32 bit version of powershell.
Have you tried running as Admin? I believe Process.Start requires elevated privileges that are not available in, say, sandbox mode.

Deploying windows application in Visual studio 2010 Express

I have developed a windows application on C# in Visual Studio Express 2010. Now I want to deploy it.
I used publish to deploy but when I run the project it get error on the form where I have used some external files which I am using for I/O operation, read and write files.
How do I resolve this error? I am also using one folder for files.
I'm a big fan of using WiX for installers - even if you've got Visual Studio Pro with the built in MSI creation tools
http://wix.sourceforge.net/
the benefit of WiX is that you can do most anything, from the super simple to very complicated. The tool is great b/c you can get started quickly and then move up (adding dialogs, etc) as your app gets more popular.
One trick for installers - consider using VMWare/HyperV, install a test OS then take a snapshot of the OS before you install your app. IF everything works, great - you're done. However if something isnt right, roll the OS back to the pre-install snapshot, fix the bug, and try it again.
Add those external files to the project
Right Click the external files in your VS Set the Build Action as Content
Copy to Output directory to CopyAlways
Then try deploying
That may be due Windows 7 UAC permissions. Despite the fact that current books, msdn and even the snippets in Visual Studio teach methods of file IO, most are not correct when taking UAC into consideration.
All of your file operations should take place in UAC safe zones such as
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
You cannot read or write to files in the C:\Program Files (x86) directories unless you have elevated your app to run with higher privileges.
If you install your application and right click the executable and select Run As Administrator and everything works the problem is UAC.
If you haven't coded around UAC limitations I highly recommend reading up on it. It will save many headaches down the road.
http://www.codeproject.com/Articles/17968/Making-Your-Application-UAC-Aware

prompt to reboot computer after installation completed

I am currently adding a deployment project to my C# solution which
installs a windows service and I have been trying to find a property
that will make the installer prompt the user to reboot the system once
installation has completed. I would prefer to simply set a property
rather than create a small application that I run at the end of the
install.
if there is any code to restart then where i'll use this code
in installer class or elsewhere?
If you use a MSI-based installer (which would be the case if you're using a VS setup project), setting REBOOT=Force should do the trick.
Under normal circumstances, though, the installer will itself detect if a reboot is needed: if you're not currently getting a reboot prompt, that's a good indication your service should work just fine without that reboot.
You may be able to extend your setup logic to start the service after installing it (and also to launch any auto-start GUI components if required). That's much friendlier than forcing a reboot, and you'll do your bit to help Windows get rid of its "you moved your mouse, please reboot" image problem...
Edit (December 2011) #IdentityCrisisUK: see HOW TO: Control System Restarts in a Setup Project That You Created by Using Visual Studio .NET for the exact steps involved in setting the REBOOT variable. Use of Orca is required -- not sure why that has "already been ruled out", as it's a trivial postbuild step...
Use the REBOOT Property of WIX to restart prompt in the Product.wxs file of your Setup. Syntax is :-
<Property Id="REBOOT" Value="Force"></Property>

Categories