Can't delete PDF files programmatically - c#

I can't delete a pdf file from a shared drive (Windows mapped drive) from my c# Winform desktop program which runs on Windows 10 OS. The pdf file was created by Crystal Report in my c# program and when there is a revision, I need to delete the old pdf file and write the new one. I will get access denied error (HResult=0x80070005) when the program tries to delete the file. This only happens when the I try to delete it from a shared drive. If I generate the pdf file in the local drive (c:), and delete it from the local drive, then everything is find. I can manually delete the pdf file from File Explorer, but not from my c# program.
In addition, if the file is not a pdf file, then there is no problem to delete it from the shared drive.
It is weird that this only happens to pdf file or a file with .pdf extension. If I create a simple text file (test.txt) with C++ and save it in the shared drive, I can delete it without any problem. But when I create the same text file and name it as test.pdf, then I can't delete it.
HResult=0x80070005 seems only links to permission issue, but I strongly believe my case is NOT a permission issue because if the file is *.txt, then I can always delete it without any issues.
To rule out the possibility of the pdf file that may be still linked with the Crystal Report or opened by some programs, I copied the pdf file from the shared drive and save it in my local drive (c:) and wrote a simple c++ program to test if I can delete it, and it failed. Can't delete it. I can only delete it if the pdf file was created and saved in the local drive originally. And if the pdf file is copied from the shared drive to the local drive, then I can't delete it.
I would appreciate it if someone can shed some light on it.

I think (you/your app) don't Have Permission To Delete a File
Go to the folder that contains the PDF File.
Right-click on that folder and choose Properties.
Navigate to the Security tab.
Click on Edit, then click Add.
Under the Enter the object names to select box. Type
"Everyone"(without quotes) and click on Check names. Click OK.
A new window will open up with the Security tab. Under Permissions for
Everyone, check the Full control option. Select Apply and OK.
Ownership is already allowed for the folder.
Now you Can delete PDF files programmatically
Don't Have Permission To Delete a Folder

Thanks for trying to provide solutions for this problem. With a help from a friend, I finally found the root cause of the problem. It is the Norton antivirus that prevents me from deleting a certain types of files. .pdf is one of them. After I unchecked the .pdf from Norton, it works like a magic.
It is the antivirus program that prevents me from deleting .pdf file programmatically.

Related

How to Open a file in Unity Android?

I need to open a kmz file from the mobile app in order to see it in google earth, So i saved the kmz file in the StreamingAssets and i copy it to the persistentDataPath, Also the file is unable to open when i copy it to this folder.
Im calling it with this:
Application.OpenURL("file://"+Application.persistentDataPath + "/CR.kmz");
I already granted the file permissions.
I dont know how to open files from the storage with code. Help.

Create app with data already present on install

I'm currently programming an app that need to access to some Excel files.
So what I need is to create a folder in the app files with these Excel files in it but I want that folder to be created at the app install, so they would be accessible for every device that install the app.
The files also need to be modifiable in the future by the user.
The problem is that I don't know how to do it right. Should I just create a new folder in the Solution Explorer and put the Excel files in it ? Should I create the folder programmatically and force the user to put them manually in that folder ?
I don't really know how to do it so that the application will not be too complicated to be modified by the user.
EDIT : Also, if I put the files in the Assets, will the user be able to change them later ?
So what I need is to create a folder in the app files with these Excel files in it but I want that folder to be created at the app install, so they would be accessible for every device that install the app. The files also need to be modifiable in the future by the user.
For your requirement, you could use ApplicationData.LocalFolder to store Excel files, LocalFolder has full access permission. LocalFolder exists in the app's sandbox path and will be created after the app is installed.
Also, if I put the files in the Assets, will the user be able to change them later ?
Assets folder exists in Windows.ApplicationModel.Package.Current.InstalledLocation and it is read only that often use to store some static resource. You can't modify the file at run time.
For more details about file access permissions please refer this document.
Forget the installer. It is deprecated and the new apps on Windows are installed over the store.
If you want to copy some files (Excel templates), you have to put them in your resources. At startup you can check if there is a folder in your App-Data folder with your files, if you don't find them, you can copy it from your resources. So even if the file is deleted, your app can copy them in the next start.
If you use Windows Forms with .NET, you can check this page:
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.application.localuserappdatapath?view=netframework-4.8
If you are writing an UWP-app, check this page:
https://learn.microsoft.com/en-us/windows/uwp/design/app-settings/store-and-retrieve-app-data

After using InstallShield to install my app my sqlite .db file is being saved in the AppData directory. Why is that?

After using the free version of InstallShield and installing my app on my machine I noticed that my data file (a sqlite .db file) is being saved in a different location than when I run it under Visual Studio. Basically instead of being saved in the directory with the .exe file it is being saved here:
C:\Users\blaaah\AppData\Local\VirtualStore\Program Files (x86)\MyAppA\MyAppA
My .exe file with its .dll files is being saved here:
C:\Program Files (x86)\MyAppA\MyAppA
I am curious as to why that is happening?
I am also curious on how to get that directory that contains my data file with code.
The VirtualStore folder is caused by file system redirection done by UAC. By doing this, Microsoft was able to lock down the Program Files directory without sacrificing too much backward compatibility. Any time an application tries to write to the program files location, the write will be redirected to the Virtual Store.
A decent writeup on this on MSDN can be found in the User Account Control For Game Developers article.
To quote that article:
Virtualization affects the file system and registry by redirecting system-sensitive writes (and subsequent file or registry operations) to a per-user location within the current user's profile. For example, if an application attempts to write to the following file:
C:\Program Files\Company Name\Title\config.ini
the write is automatically redirected to:
C:\Users\user name\AppData\Local\VirtualStore\Program Files\Company Name\Title\config.ini
Likewise, if an application attempts to write a registry value like the following:
HKEY_LOCAL_MACHINE\Software\Company Name\Title
it will be redirected instead to:
HKEY_CURRENT_USER\Software\Classes\VirtualStore\MACHINE\Software\Company Name\Title

XML saved by application installed in Program Files: where is it?

I've spent the last two hours Googling on this with no results, so…
I am developing an application which uses XML files to save data. These files are supposed to exist in the same directory as the program executable. I use:
string filePath = System.IO.Path.GetDirectoryName(Application.ExecutablePath) + "\\xml\\filename.xml";
It works.
I've created the installation file in Inno Setup. This works as well, copying my XML files exactly where I want them to go.
I open my application directly from the installation folder, from the Start menu icon, from a desktop shortcut, and it works fine.
I make some changes to my program, then I close it and start it again. Everything is fine: all the saved changes are read back from the XML.
But then I open the specified XML file and there are no changes!
I wouldn't bother as at least it's working, but:
When I uninstall the application then install it again to the same directory, changes made to the previously installed file are still there!
I'm very curious to know what's happening.
This only happens if the program is installed to Program Files. If I install it to My Documents, changes are shown in the XML files and after reinstalling it the default settings are restored as expected.
My questions are:
Where are those XML files being stored, and how can I load it if the specified path points to Program Files and they are not there?
Obviously, how do I fix it?
EDIT
Finally found those files in C:\Users[USERNAME]\AppData\Local\VirtualStore\Program Files[APPNAME]
Check what is saved into your local application data folder (it's \Users[USERNAME]\AppData\Local\ for Windows 7).
It looks as if your app didn't have rights to save things in your ProgramFiles folder and saved it where it could, probably in this location:
String appData =
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
Also you may try running your application as administrator and check if changes to your xml data still doesn't show.
Cant you print out the value of
System.IO.Path.GetDirectoryName(Application.ExecutablePath)
then you should be able to see where your xml files are stored, if it is a console application just use Console to print if it is WPF print it to a label or something

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