0

After installation the application gives error

access denied to the file PathName\\BLsms.ocx

Here PatheName is complete path.

Advance installer is used to create the installer.

Then application is installed in C Drive.

How can I make these files read and writable?

I am using this code.

try
{

    this.brlmfc = new BinaryReader(new FileStream("BLsms.ocx", FileMode.Open, FileAccess.ReadWrite,FileShare.Read));
    this.brocx = new BinaryReader(new FileStream("BLrpi.lfc", FileMode.Open,FileAccess.ReadWrite));

    this.brlmc = new BinaryReader(new FileStream("rpcgh.lfc", FileMode.Open,FileAccess.ReadWrite));
    this.passkey1 = this.brlmfc.ReadString();
    this.passkey2 = this.brocx.ReadString();

    this.passkey3 = this.brlmc.ReadString();

}
catch (Exception e13)
{
    MessageBox.Show(e13.Message);
    i = 0;
}

I have also tried the following code in Form Load method.

FileIOPermission f2 = new FileIOPermission(FileIOPermissionAccess.AllAccess, Application.StartupPath+"\\BLsms.ocx");
f2.AddPathList( FileIOPermissionAccess.Read, Application.StartupPath+"\\BLrpi.lfc");
f2.AddPathList( FileIOPermissionAccess.Read, Application.StartupPath+"\\Brpcgh.lfc");
f2.Demand();
  • 1
    are you getting correct path using `Application.StartupPath+"BLsms.ocx"` or you have to use `Application.StartupPath+"\BLsms.ocx"` – Sandeep May 01 '15 at 06:55
  • yes, I after installing the files. The path is C:\Program Files (x86)\MyFolder\Myapp\BLsms.ocx – Pretty Doll May 01 '15 at 07:00
  • 1
    please try `FileIOPermission f2 = new FileIOPermission(FileIOPermissionAccess.AllAccess, Application.StartupPath+@"\BLsms.ocx");` also for other two files – Sandeep May 01 '15 at 07:10
  • @PrettyDoll [Check this link](http://stackoverflow.com/questions/16849929/file-access-denied-while-accessing-from-program-files). It's may help you. – Krunal Mevada May 01 '15 at 07:19
  • I corrected this. But I am having still the same issue. This path should be as you told. – Pretty Doll May 01 '15 at 07:21
  • 1
    @PrettyDoll [Check this link](http://stackoverflow.com/questions/7288444/how-to-give-read-write-permissions-to-a-folder-during-installation-using-net). It's give useful info. – Krunal Mevada May 01 '15 at 07:32
  • @PrettyDoll have you checked which permission are set right now to file? or try [Give Full Access](http://stackoverflow.com/questions/17763570/change-a-files-acl-to-allow-full-access-for-everyone) – Sandeep May 01 '15 at 07:45
  • @PrettyDoll please accept answer/comment by which you get idea how fixed your problem. – Krunal Mevada May 01 '15 at 08:35
  • @smoothersillent How can I accept the comments? Today is my first day of using it. I cant even vote. It says, I need more reputation. – Pretty Doll May 01 '15 at 08:43

3 Answers3

2

I assume you are installing the program into C:\Program Files or C:\Program Files (x86). Don't write in those directories. Use C:\ProgramData or the User's home directory instead.

Robert McKee
  • 21,305
  • 1
  • 43
  • 57
  • Yes, you are right. If I write the files in other path then these files will be accessible. But I have to keep all files here and access the files here. I want to grant permissions manually in the code. – Pretty Doll May 01 '15 at 06:54
  • It is not appropriate to store data in the Program Files directory. You would need administrator rights to do that, and user applications should not have administrator rights. Program Files is for program CODE only. Nothing but installer programs should ever be writing anything into those directories. You need to rethink your process. – Robert McKee May 01 '15 at 06:56
  • @PrettyDoll Why you sticky with `C Drive` ? – Krunal Mevada May 01 '15 at 06:59
  • Again you are fright Sir. But we know most of the software store their own data in the program files. I have also checked even Matlab and Visual Studio. If I install this in other drive then it works fine. But as we know the application should go to C drive therefore right now I am here in C – Pretty Doll May 01 '15 at 07:06
  • @PrettyDoll So, you can set programmatically permission on your file `BLsms.ocx` when your trying to read. – Krunal Mevada May 01 '15 at 07:12
  • @smoothersillent I have tried to set programmatically as given here https://msdn.microsoft.com/en-us/library/system.security.permissions.fileiopermission%28v=vs.110%29.aspx but unable set permission. – Pretty Doll May 01 '15 at 07:27
  • 1
    @PrettyDoll Most programs definitely do not store their own data in the program files, and Visual Studio definitely does not -- Visual Studio stores its data into C:\Users\{username}\AppData\Roaming\Microsoft\VisualStudio – Robert McKee May 01 '15 at 07:27
  • 1
    It also stores some data into C:\Users\{username}\Documents\Visual Studio as well (mostly project files though). All the plug-ins, and settings are stored in AppData\Roaming directories – Robert McKee May 01 '15 at 07:34
1

I have set read/write permissions of the files from Advance Installer. And works fine.

http://www.advancedinstaller.com/user-guide/permission-dialog.html

Thanks for everyone.

  • 1
    Please don't copy this link to other posts, not without further tailoring to the question. Your other answer was flagged as astroturfing. See the [help center](https://stackoverflow.com/help/promotion) to avoid being pegged as a spammer again. – Martijn Pieters May 01 '15 at 09:05
  • 1
    I will re-iterate that this is a terrible approach. There is a reason what you are trying to do is difficult. It shouldn't be done. It breaks many (most) backup strategies that don't backup the Program Files directories because the only things that should be in there are code and immutable after installation. You may also find that certain anti-virus/anti-malware detects these changes and starts throwing warnings as well. You'll also have problems if you ever need to run your code in the cloud, in a terminal services environment. – Robert McKee May 01 '15 at 16:02
  • You will also have issues if you try to deploy this into an environment that uses roaming profiles as well if you expect the settings to follow the user. – Robert McKee May 01 '15 at 16:05
1

By default Users group doesn't have write access in per-machine locations like Program Files. This is a Windows standard which is not related to installations. However, during install you can set any permissions you want.

Windows Installer does support custom permissions, but Visual Studio doesn't offer a way for setting them. So the only solution in Visual Studio is a custom action.

Unfortunately Visual Studio doesn't support attached custom actions. So using XCACLS.EXE to set permissions would work only if you include it in your package (it will be installed on the target machine along with your files).

A cleaner, but more complex solution is to write a custom action yourself (using custom code) to set the permissions you want.

The fastest and cleanest solution would be to use a different setup authoring tool which offers more control over permissions.

Krunal Mevada
  • 1,637
  • 1
  • 17
  • 28