tags:

views:

607

answers:

3

Hello,

I'm writing a program in c# for Windows7 that works very fine... But now I started to build a setup that copies the program files into "C:\Program Files".

Now there are a lot of Problems when the program is in that folder:

1) If I cancel an OpenFileDialog I'll get an exception

2) My program don't write files into the AppData folder anymore

3) The program can't open intern files in its own directory because of the permission

I don't know what i can do...

Can someone help me?

EDIT:

Maybe you didn't understand my problem.

I wrote a program that works fine in C:\myprogram. I made an installer that copies the files into the C:\Program Files directory, it's the same when I copy my files into that directory

  • My program only opens files in its own directory
  • My program opens and writes files in the AppData folder
  • My program can open files like .txt in a rtb. There the OpenFileDialog will be used

This 3 points don't work!

If my program is in the Program Files folder it can't open a file like C:\Program Files\myprogram\xsl\test.xsl and can't write a file into the AppData folder.

If I install my program into C:\lalala it will work!

+1  A: 

To copy files into Program Files or any privileged location, the process must be run by an elevated administrator. Since you are talking about "copying" files and an "OpenFileDialog", it sounds like you are running a .NET process to do the copying, rather than a Windows Installer. Usually, this should be done by an installer rather than your app. Your app needs to set requireAdministrator in its manifest or elevate just for that particular action. For more info, you should read up on UAC. As a start, I suggest you read UAC: The Definitive Guide on CodePlex.

flipdoubt
+1  A: 

You really should use a windows installer program. It takes care of the issues related to instasllation and makes sure that its privilege level is elevated to do the work that is necessary.

UAC will do lots of things behind the scenes if you aren't properly elevated that can completely change the way your program sees the filesystem.

Also, when you open files that are in your program directory, you can only open those files in read-only mode, or yo uwill get a file exception.

Mystere Man
+2  A: 

Ok - I found the answer:

I have to use

FileStream fs = File.OpenRead(tmpfile)

instead of

FileStream fs = new FileStream(tmpfile, FileMode.Open);
zee