1

I have a program which contains a local database It works fine when im running it from a non-system Directory , but when im running it from a system directory like program files directory i cant access to my database Im using c# wpf . Have no idea how to access my database :/

tho im using an Entity Data Model which works with that database in application directory (which is going to be in program files Directory Like Every other application i install e.g Adobe applications MS Office etc .. ) how do they have access to their datas in program files directory without getting any permission from user ?!!! :((

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Mahdi.KH
  • 13
  • 1
  • 6
  • 1
    Run the program/Visual studio as an administrator? – Christoph Sonntag Jan 02 '19 at 13:36
  • 3
    You shouldn't store data in the program files folder. Use something like: Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) and store the data there. – Kevin Jan 02 '19 at 13:37
  • @Compufreak i cant force each User to run the program as administrator bruh the problem is that i want the program grant that access itself or at least ask the user only once to grant it manually – Mahdi.KH Jan 02 '19 at 18:04
  • @Kevin yeah sure that'll do the trick but i'm using an Entity data model which works with the database which is in application directory :(( ... im using stored procedures dude – Mahdi.KH Jan 02 '19 at 18:06

4 Answers4

2

You should not save program data in your programs application folder as you do not have access to it without changing the users permissions or using administrative permissions. You should use an Environment.SpecialFolder for it.

E.g.

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\YourApplication"

This is also what e.g. Adobe Flash or MS Office are using.

Christoph Sonntag
  • 4,459
  • 1
  • 24
  • 49
  • thanks but how can i create an entity model which works with my database in a different folder than my program folder ? currently i have an EDM which is working with my database placed in my program folder how can i change its Connection string ?! is it possible in Run-Time ? – Mahdi.KH Jan 02 '19 at 18:41
  • or how can i Copy my database in that special folder when i have no access to copy it ... is it possible to place it there when installing the program on client machine ? i'm using InstallShield to deploy my program – Mahdi.KH Jan 02 '19 at 18:44
  • It depends on the type of database you are using, you should read up on how to configure the connection string for your version of the entity framework, probably like mentioned [here](https://stackoverflow.com/questions/5299775/how-should-i-edit-an-entity-framework-connection-string). – Christoph Sonntag Jan 02 '19 at 18:47
  • Yeah that's what im looking for . But one last thing is that can i use a dynamic connection string instead of a static one in app.config ? Bcz i dont know whats the name of user to use the applicationData folder on client machine in xaml file ! Maybe binding or sth ...? Im using a local sql db file (mdf) btw – Mahdi.KH Jan 02 '19 at 19:07
  • I usually only use EFCore, not EF so I don't know, sorry - this sounds like you should ask it in another question as it's not really related to your initial issue. – Christoph Sonntag Jan 02 '19 at 19:09
1

I am posting this as a answear because I cannot comment yet, however I would check the following:

  • Permissions, if the program is run by a different user, that could lead to problems
  • Paths, make sure that they are not relative

But without the code, it's hard to tell

  • thanks bruh but how can i find out that this user is an administrator ? or a different user ? or how can i find out that they're running my program as administrator ? – Mahdi.KH Jan 02 '19 at 18:23
1

Without error-messages it's hard to guess what your problem is.

My guess: You need administrator-privilegues to write to C:\Program Files.

Data which is changed by your program (Files oder DBs) should not be contained in this directory!

This is a security-mechanism to protect your programs (all not only which you did create) from being changed by other users.

kara
  • 3,205
  • 4
  • 20
  • 34
1

EDIT : Here the code to use app data folder

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        GetDB();
    }

    void GetDB1()
    {
        var DBFile = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "DB1");
        var con = new System.Data.SQLite.SQLiteConnection($"Data Source={DBFile}.sqlite;Version=3;");
        con.Open();
        string sql = "Select 1 as col1";
        var command = new System.Data.SQLite.SQLiteCommand(sql, con);
        var reader = command.ExecuteReader();
        while (reader.Read())
            Console.WriteLine("col1: " + reader["col1"]);
    }
}

simply on window check your folder permissions like in pictures


more details

1. Software

software is a collection of data or computer instructions that tell the computer how to work

  • for simplicity there are two main types

    • operating system software

    • Application software

2. operating system software

An operating system (OS) is system software that manages computer hardware and software resources and provides common services for computer programs.

3. Directory structure

In computing, a directory structure is the way an operating system's file system and its files are displayed to the user. Files are typically displayed in a hierarchical tree structure.

4. Windows 10 [username]\AppData

This folder stores per-user application data and settings. The folder contains three subfolders: Roaming, Local, and LocalLow. Roaming is for networked based logins for roaming profiles. Data saved in Roaming will synchronize to the computer when the user logs into that. Local and LocalLow does not sync up with networked computer

5. Guidelines : Store and retrieve settings and other app data

  • App data is mutable data that is specific to a particular app. It includes runtime state, user preferences, and other settings.

  • App data is different from user data, data that the user creates and manages when using an app. User data includes document or media files, email or communication transcripts, or database records holding content created by the user. User data may be useful or meaningful to more than one app.

  • Often, this is data that the user wants to manipulate or transmit as an entity independent of the app itself, such as a document.

-Important note about app data: The lifetime of the app data is tied to the lifetime of the app. If the app is removed, all of the app data will be lost as a consequence. Don't use app data to store user data or anything that users might perceive as valuable and irreplaceable. We recommend that the user's libraries and Microsoft OneDrive be used to store this sort of information. App data is ideal for storing app-specific user preferences, settings, and favorites.

6.Access control

Access control refers to security features that control who can access files (resources) in the operating system (OS).

7.what is the relationship between operating system and application software

  • All Applications call access control functions on the operating system (OS) to access specific resources or control access to resources provided by the application.

  • So your App Need you as a user who has a control over the operation system to ask the operation system for Folder permission

  • So you need C# code + OS permission

8. How To apply this concepts

  • 8.1. Run as administrator because they have accesses
  • 8.2. Check the folder that the App is in
  • 8.3. Add permission To the Folder for the current user

8.1. Run as administrator because they have accesses

8.1.1 How

enter image description here

8.1.2 Why

enter image description here

8.1.3 Why NoT

Why you shouldn’t run as admin… – Aaron Margosis' Non-Admin, App-Compat and Sysinternals WebLog

If the exploit happens to be written so that it requires admin privileges (as many do), just running as User stops it dead. But if you’re running as admin, an exploit can:

  • install kernel-mode rootkits and/or keyloggers (which can be close to impossible to detect)
  • install and start services
  • install ActiveX controls, including IE and shell add-ins (common with spyware and adware)
  • access data belonging to other users
  • cause code to run whenever anybody else logs on (including capturing passwords entered into the Ctrl-Alt-Del logon dialog)
  • replace OS and other program files with trojan horses
  • access LSA Secrets, including other sensitive account information, possibly including account info for domain accounts
  • disable/uninstall anti-virus
  • cover its tracks in the event log
  • render your machine unbootable
  • if your account is an administrator on other computers on the network, the malware gains admin control over those computers as well
  • and lots more

8.2. Check the folder that the App is in

In Windows chec

enter image description here

enter image description here

8.3. Add permission To the Folder for the current user

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

Ref.

Mohamed Elrashid
  • 8,125
  • 6
  • 31
  • 46