Application creating / deleting local xml files when installed - c#

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

Related

How to create text file in .NET project folder, not in bin/Debug folder where is by default

I have a very simple .NET console application in Visual Studio. I am trying to write some words into a text file.
using (StreamWriter file = File.AppendText("log1.txt"))
{
file.WriteLine("Hello from the text file");
}
If the file does not exist, the application creates it in the autogenerated folder bin/Debug.
Is there a way to create this file in the project's directory, where I have .csproj file?
And more important, in real-world applications, when you work with files, you keep them in bin/Debug? That's why .NET creates them there firstly?
Is there a way to create this file in the project's directory, where I have .csproj file?
Yes, but this can only be done while you are working on your project. Once you are done developing it and try to publish it you won't have access to the location where you have .csproj file, because after publishing you can install it on any PC and it wont have the project you are working on.
And more important, in real-world applications, when you work with files, you keep them in bin/Debug?
No, I assume by real-world applications in your context you mean a published project '.exe' that you can run on any PC. Windows provides you three Data folders that you should use when writing your program so that it works smoothly after publishing:
User Data
Roaming User Data
All User Data
You can acess the above folders in .NET application using the Environment.SpecialFolder:
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
As per your given code, try this :
var fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"log1.txt");
using (StreamWriter file = File.AppendText(fileName))
{
file.WriteLine("Hello from the text file");
}
This way you will be able to publish your program and it will still work smoothly without hard-coding the path as you were doing previously.
That's why .NET creates them there firstly?
If you don't specify a complete path, and just the file name .NET looks into the working directory of the executable, which in this case is bin/Debug
Is there a way to create this file in the project's directory, where I have .csproj file?
Yes. As explained here (second answer) you can use the post-build event to write down the value of $(ProjectDir) in a text file (using command echo $(ProjectDir) > ..\..\projectdir.txt). This macro contains the directory of your .csproj. This command will create the file projectdir.txt with your project directory after a build process so you read this file contents in your code and use what is inside it to pass to File.AppendText as the base directory to create your file log1.txt.
And more important, in real-world applications, when you work with files, you keep them in bin/Debug? That's why .NET creates them there firstly?
That depends on what you want to do. In your case the code creates the file at bin/Debug because that is where your executable are being executed. When you omit the full path to File.AppendText and just pass "log1.txt" as argument, it will create the file in the same folder as the executable are at. If you want a different folder you should specify the folder here (e.g. File.AppendText("C:/log1.txt") will create the file at C:/.
You can create the text file in the root of your project and use copy always to have them in the same place as your executable. If this is just a readonly text file then it's OK because windows doesn't allow you to modify the files reside in Programs folder in OS drive.
If you want your code to modify these text file then you need to put them in appdata folder. In real world example I did this on many project. All the database work my winforms, WPF application need goes in AppData folder.

How to access a folder in c# in application files which is created after publishing application using relative path?

I am creating a application where I need to access images from a image folder in application files which I will add later after publishing the application. But the thing is when I access my application data path it is .../AppData/Temp...
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
So what is the way to access the data in Application Files in .exe directory.
My base directory is in Temp. But I want to access the one from where I executed the .exe
See more How can I get the application's path in a .NET console application?
Sulotion:
Path.Combine(System.Reflection.Assembly.GetExecutingAssembly().Location,#"/Images/logo.png");
I hope this helps you.

how to publish C# application with the access database

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.

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";

Folder and file not added to User's Application Data folder

Ladies and Gentlemen , I have been stuck with this for a few hours and do not find an answer. I have a Setup project in Visual Studio that creates an installer for my C# application. What I want is to add a folder with an XML file from which my application can read and write to the User's Application Data folder. In the File System Editor window I added the User's Application Data folder. In this folder I added a new folder (renaming it to my app's name) and then place the XML file in there. I also set the AlwaysCreate to true for the folder. The installer should create the folder in C:\Users\UserName\AppData\Local and add the file to it. However, the installer does not create the folder or the XML file my application uses. What am I missing? Is there another way to install a read/write XML file? Thanks in advance!
Ok, I found what the issue was. If a file is added to the User's Application Data folder it is installed on the target computer at C:\Users\Username\AppData\Roaming and not into AppData\Local.
Therefore, I changed my application get the file from Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) instead of Environment.GetFolderPath( Environment.SpecialFolder.LocalApplicationData).
Hope it helps someone else...

Categories

Resources