-1

I have written an executable that copies files from a networklocation to a folder in Program Files. When I run this normally (as administrator), everything goes fine. However, when running it as a startup program, execution suddenly stops.

There is no exception thrown (to my knowledge, I have included logging which works when executing normally). It just seems as it stops running; I have added a messagebox to be shown after execution but this will not work.

Is there any solution in code or in settings to get my startup-program to write to the Program Files?

I can provide code if needed, but it's just a simple file.CopyTo() method. The exact path is: C:\Program Files (x86)\Microsoft Visual Studio 11.0\Team Tools\Static Analysis Tools\FxCop\Rules

Additional code:

string sourceLocation = ConfigurationManager.AppSettings["sourceLocationCodeAnalysisRules"];
DirectoryInfo sourceDir = new DirectoryInfo(sourceLocation);
sourceDir
        .GetFiles()
        .Where(file => file.LastWriteTime > new FileInfo(Path.Combine(deployDir.FullName, file.Name)).LastWriteTime)
        .ForEach(file => file.CopyTo(Path.Combine(deployDir.FullName, file.Name), true));
}

The purpose of this executable is to check the networklocation for newer files, and if they are newer, copy them to the given location. So an installer is not something I need.

Matthijs
  • 3,162
  • 4
  • 25
  • 46
  • 1
    Sounds like a UAC issue - you should instead create an MSI installer (there's an express edition of Installsheild available with Visual Studio) – Rowland Shaw May 12 '14 at 13:31
  • How do you run it as startup Program? by Registry or by putting it into the Autostart Folder? – BigM May 12 '14 at 13:32
  • @RowlandShaw: What would be the exact use of this? Is there no way to have my .exe file write to it directly? – Matthijs May 12 '14 at 13:33
  • @BigM: I have tried both ways, neither worked. – Matthijs May 12 '14 at 13:33
  • 1
    In short, UAC is a security measure to minimise the risk of malware copying itself somewhere that looks "safe". It sounds like you're trying to fake an installer, but you'd be better off actually writing one, and installing once. There are other ways around it (such as changing the folder permissions), but they would increase the risk of a security issue later on, and not something I would recommend. – Rowland Shaw May 12 '14 at 13:38
  • @RowlandShaw: I am not trying to fake an installer; it is a program designed to check for files on a network location. If their creationdate is newer than its' Original in the program files, it has to copy and replace for the newer version. – Matthijs May 12 '14 at 13:51
  • That *sounds* like an installer, updating the installation of files, to me. – Rowland Shaw May 12 '14 at 14:00
  • @RowlandShaw: So an installer that runs every time on startup is the answer here? – Matthijs May 12 '14 at 14:01
  • You deploy the installer (via Active Directory, or otherwise) when you deploy a new version. – Rowland Shaw May 12 '14 at 14:03

4 Answers4

1

You probably have to run it as admin. I guess you added the program to Startup in All programs of Windows. In that case, open properties of the shortcut of the startup file and in Compatibility tab check 'run this program as administrator'.

In any case, if this does not work, you should add logging to your app to see what happens.

EDIT: Alternatively you could schedule as task with Task Scheduler, executed with trigger 'When the computer starts'. It seems to work.

EDIT 2: it seems you need a manifest file in order to be able to copy to program files.

Community
  • 1
  • 1
L-Four
  • 13,345
  • 9
  • 65
  • 109
  • This I have also tried, my program did not execute anymore after changing its compatiblity. I am an administrator. – Matthijs May 12 '14 at 13:36
  • Then you should add logging to your app and see what happens. Is there sth in event viewer? – L-Four May 12 '14 at 13:37
  • I have added logging already, as you can see in my question. The execution of the program just doesn't happen, if I change the compatiblity settings to run as admin. I also checked the eventlog for clues but did not discovery anything. – Matthijs May 12 '14 at 13:40
  • See my EDIT: you could try Task Scheduler. – L-Four May 12 '14 at 13:43
  • Would this get rid of the administratorissue? – Matthijs May 12 '14 at 13:46
  • You can quickly try out and let us know :) – L-Four May 12 '14 at 13:47
  • Are you logging the start of the application? Are you sure it doesn't start? Did you do a try-catch in the entry point of your app and log the exception in the catch? – L-Four May 12 '14 at 13:58
  • Yes, I am using a try-catch construction. I have even added a messagebox at the start, trying to indicate if the executable started. It did not show. – Matthijs May 12 '14 at 14:00
  • And - as a test - if you put the application in another folder (instead of program files)? – L-Four May 12 '14 at 14:02
  • The application itself is not in Program Files. – Matthijs May 12 '14 at 14:02
  • Sorry I meant change the folder to where the files are copied, just as a test to see if program files folder has anything to do with the issue. – L-Four May 12 '14 at 14:08
1

Instead of putting your application in the "Startup" folder and have it start as Admin (with the annoying UAC prompt at startup), make a Task will be in charge of doing that.

You can see here how to do :

You can make sure everything is set up correctly with a Windows Installer.

gretro
  • 1,934
  • 15
  • 25
0

You can't change the contents of the Program Files folder when not running as Administrator.

At best, you have to force your application to run as Administrator, but then every time the program runs, you will get a UAC message.

If this is what you want, edit the app.manifest according to this answer: How do I force my .NET application to run as administrator?.

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • I do not want a UAC message, I have checked this function already, this is not wanted performance. – Matthijs May 12 '14 at 13:34
  • 1
    Then don't change the Program Files folder. Even a regular user has to go past this UAC message when copying to that folder. – Patrick Hofman May 12 '14 at 13:34
  • In this case I have to, the program is designed to copy files from a networklocation to the Visual Studio Rules location (Code Analysis/FxCop). I would not be asking for a solution if it wasn't required ;) – Matthijs May 12 '14 at 13:35
  • Then check if it is really necessary to do so by comparing the files. Then you can minimize the amount of tries. – Patrick Hofman May 12 '14 at 13:36
  • 1
    Or do this at installation time if possible. – Patrick Hofman May 12 '14 at 13:38
  • The program has to check for newer versions of these files on startup; which is why this function is key here. I have added the exact path for reference. – Matthijs May 12 '14 at 13:38
  • Well, check it, but don't change it until necessary. If necessary, elevate and change. – Patrick Hofman May 12 '14 at 13:39
  • I am checking if change is necessary; see additional code. It checks if the file to be copied is newer than it's Original located in programfiles. – Matthijs May 12 '14 at 13:43
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/52524/discussion-between-matthijs-and-patrick-hofman) – Matthijs May 12 '14 at 13:55
0

As I understand you want you to run "code" a user will need special permissions to run. All I can think of that could help you in this scenario is that you set the permission restriction in code and this will throw the right exception like:

[PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]

In your scenario it will be something like:

[FileIOPermissionAttribute(SecurityAction.Demand, Read = "C:\\Program Files")]

You can find a full example an explanation here:

How to demand permissions by using Code Access Security.

Dalorzo
  • 19,834
  • 7
  • 55
  • 102