how to publish C# application with the access database - c#

I created a C# application which run with Microsoft Access database and after I deployed the project and installed it on C drive the database file becomes read only, and, if I install it on D or another drive it works fine.
Please if any one can help it is appreciated (SIS is access database file) the problem is i want to make it work in C drive also.
this is my setup SIS is the access file
And this is the connection string im using
String cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\SIS_DB.accdb;";

Your problem is your database file is in %ProgramFiles%. It should be in %AppData%
There are two ways to resolve
1.modify the setup project.
when you make the setup,you should specify the path of f.mdf,ensure that the file will install into AppData folder.
2.copy f.mdf to AppData folder by app.
every time you run you app,the first thing is to copy the file to AppData folder,
you can add the follow code in your Main(or init) method and try again:
string sourcePath=#"C:\PROGRAM FILES\DEFAULT COMPANY NAME\SETUPER2";
string appDataPath= Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string fileName="F.MDF";
System.IO.File.Copy(sourcePath+"\\"+fileName, appDataPath+"\\"+fileName ,false);
*1 is better.

You are old school ... the C drive these days is forbidden area.
Use either the Program Data folder for application specific data - or, for user data, the %AppData% folder where you create a folder for your application and use this folder for the user's data.

Related

C# - Copy a file to user folder

I'm making the GUI program that users can decompile/recompile an APK file but compiling won't work correctly becuase the framework file is missing. I'm making the simple framework installer that must be installed on user folder (for apktool.jar). Instead using the path that only works on my computer. i want to make it work for all users.
here is the code i made
File.Copy(#"do-not-touch\1.apk", #"C:\Users\quoc\apktool\framework\1.apk");
You can use the Environment.SpecialFolder Enumeration to get the path like so:
var userDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Note: Please use ApplicationData and not the root profile, it's rude to fill the user's toplevel profile with stuff.

Application creating / deleting local xml files when installed

I have an application that needs to create, read and delete 2 xml files. These files are stored in the same directory as the application file. When I run this application in debug it works fine. but as soon as you create an installer for this and run the installed program it failed to create the files. Is there any way I can set the application permission to the folder it has been installed to?
Any help would be great.
Thank you
Here is the code im using:
if (File.Exists("ApplicationData.xml") == true)
{
File.SetAttributes("ApplicationData.xml", FileAttributes.Normal);
File.Delete("ApplicationData.xml");
}
doc.Save("ApplicationData.xml");
This is throwing a System.UnauthorizedAccessException
Rather than start changing directory permissions, how about putting the files into the temporary directory? You can get the directory from Path.GetTempPath().
I would suggest you to create a "tmp" folder from your application, and then create your xml files inside . This will guarantee you that have permission on the folders, as you have created them in the privilege scope of the application.
string folderName = #"Folder";
string pathString = System.IO.Path.Combine(folderName, "SubFolder");
System.IO.Directory.CreateDirectory(pathString);
Here i've tested it out for you:
http://pastebin.com/cAmFQvee
This works fine

Modify ConnectionString in .EXE.CONFIG file with Windows7?

I have an SQL Server CE 4.0 (SDF) file that under the Users Application Data Folder (because due to Win7 UAC you cannot modify the DB if it is in the \Programs Files\ folder. But the problem remains that I need to now (on install) modifiy the .EXE.CONFIG file (ConnectionString) to point to the new path for the DB, however obviously I do not have access to modify the .EXE.CONFIG file either in the \Program Files\ folder ...
So how do people resolve this issue?
Can you deploy the .EXE.CONFIG file to the users Application Data Folder as well with the SDF file? If not how? I cannot beleive you need to grant special access rights to the \Program Files\Application folder to be able to have full access...
I am using VS2010 and using a standard Deployment Project (MSI) and I don't see anyway to deploy the .EXE in one place the the .EXE.CONFIG in another (can this be done? Is this the right solution?)
Thanks,
Use the %APPDATA% variable in the connection string and you won't have to modify the configuration file. This is also more flexible than hard-coding a path that contains a username, since it will allow all users of the computer to use your program and have their own .sdf files.
However, the environment variables are not automatically expanded in .NET, so you need to enforce it by using the Environment.ExpandEnvironmentVariable method:
// Example appSetting element:
// <add key="examplePath" value="%APPDATA%\example.txt" />
string path = System.Configuration.ConfigurationManager.AppSettings["examplePath"].ToString();
path = Environment.ExpandEnvironmentVariables(path);
System.IO.File.WriteAllText(path, "foobar");

c# WCF Server application - access to a file on the server relatively

I have a c# server application (WCF) and I have a file saved on that server and i want to access it relatively so every dev machine can work with it.
this is the file path.
C:\Users\ben\Documents\Visual Studio 2010\Projects\ myfile.xml
the project where i want to access the file from is in:
C:\Users\ben\Documents\Visual Studio 2010\Projects\ MyProject
what is the best way to access the myfile.xml (relatively)? from MyProject?
Well, when you run the project, your current directory will be something like:
C:\Users\ben\Documents\Visual Studio 2010\Projects\MyProject\MyProject\bin\Debug\
So you will probably want to do something else.
Include the file in the project and go to File Properties for the file. Select Copy Always for the Copy to Output Directory setting. The file will then be copied to the same directory as the EXE when compiling/building. That way you can access the file simply by its filename.
From your question , what is under stood is that you want to access your project location , without the executable file and folder . To do that try the following code :
string AppPath = System.Environment.CommandLine;
int pos = AppPath.IndexOf("bin");
AppPath = AppPath.Remove(pos);
AppPath += "myfile.xml";

database connectivity in c#

I am working on c# windows project.I am using Microsoft Access database with OleDbConnection connection.My database "email.mdb" is in My document directory and my "email_db.udl" is in "D: drive".This is running well on my computer.But whenever i am making an exe installer file for installment on other Pc this is not working.I am placing "mdb" and "udl" file is in same directory as they are on my PC.I am supposing this ('udl' file) is not connected with my database.
How will i resolve this problem for installation on any windows pc.
Thanks
You can put the database files in your project's directory (where sln file is) and access them through their bare filenames (without D:...., only filename).
In case of the installation you have to set the installer to put it in the installation folder (where the exe file is)
This is a good way to carry everything in one folder and be organized.
Probably here you have a problem because the "My Document" directory has a different path in every PC.

Categories

Resources