My installed WPF app is crashing while trying to create a file - wpf

I have a problem with my WPF application (a simple clicker game). I've just made a deployment/created a setup project using Microsoft Visual Studio Installer Projects extension for Visual Studio. Everything seemed to work fine, setup works good (tested on other computer) and furthermore the installed application almost works correctly. The only exception is a button calling function which is creating a new txt (something like a draft of a "save" file) file in a folder where the application is installed. Clicking this button cause instant crash of the whole application. Nothing else happens, no error message, the app just turns off.
I assume that the problem lies in access rights to the folder/administrator rights.
I've added txt file with the same name in the application folder using "File System" window in setup project, deployed and installed again. Even when the file exists the problem happend again (probably because of access rights when trying to override file content).
It's may be important that everything worked while running app through VS, also using .exe works fine on any computer. The problem is only with application installed using setup.
I'm not sure where the problem is so it's hard to show some code, to be honest i'm not even sure if it's a problem with code, maybe it can be solved with setting some setup project properties?
I would like to store some data locally between two sessions, i've choosed txt file because it's light and simple even if it's not the most elegant way.
I expect that installed application will be able to create and override txt file in it's folder.
P.S. if you need any code, screenshots or information about my setup please let me know in comments section, I will provide what's necessary

At the moment you install your program you're having administrator-rights. So the folder where your application is, also was created with administrator-rigths.
If you want to store data to this folder, you'll have to run your application as administrator. (not the best idea)
Otherwise you can change your code to write your file at runtime to a different location where you don't need administrator-rights.
For example:
string path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
will give you: "C:\Users\MyUserName\AppData\Local"
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
will give you: "C:\Users\MyUserName\AppData\Roaming"
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
will give you: "C:\Users\MyUserName\Documents"
And some error-handling around the saving of the file would be very nice and helpful ;)

Thank you everyone for solution and knowledge, the problem was as I assumed (access to the path denied).
I was using this code to create a path:
Directory.GetCurrentDirectory() + "\\SyntyhCitySave.txt";
Creating a file in a special directory instead of application folder works, that's the solution for my problem:
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\SyntyhCitySave.txt";
Tomtom answer marked as accepted.

Related

How to lock an exe file?

(Sorry in advance, if I have the wrong tags)
Hello!
So I am trying to work on a personal WPF project which would allow the client application exe to download another application exe (Unity Application) and open it within the client exe's Grid. Everything is working fine.
But, I am wondering if there's a way to "lock" the downloaded application in a way that only my client exe can open the downloaded exe?
Here's the only solution I have thought of but not sure with
Check for command line arguments within the Unity Applications
This works well but people who knows about the existence of decompiler might be able to decode that I am trying to check the arguments, and input relevant data there. :(. So, it won't help too much?
Thanks for any insights.
-Kevin
There is no non-hackable solution, you can only make it harder for crackers to break into the app by code obfuscation (hard to read the code) + adding integrity checks (make sure that your app is not modified).. There are commecial apps out there to do this (ex. PreEmptive), and their solutions differ from each other, nothing is ultimate.
Anyway, You might make Unity Application read some file or some registry value that Client application has updated just before launching the Unity Application, along with command line args method that you mentioned..

Publish WPF + EF Core App with Windows Installer Project

I have tried to create an .exe file to my WPF App and SQL Server related databse with EF Core using Windows Setup Installer Project.
I included all the dll's files from /bin/Release and built it for any CPU.
The Setup Project has been created successfully and it worked on my PC as I expected.
Otherwise when I try to install the Setup File on another PC, I can't even open it up and see what's wrong.
I guess the problem comes from the database but I can't find anything helpful on the internet.
So you get the application installed, run it and nothing happens, right? Here is what you do:
Run it. Watch nothing happen
Hit the Windows key and type "Event"
When "Event Viewer" appears in the list of applications, run it
In Event Viewer, go to Windows Logs >> Application
Near the top there will probably be an error entry. It will probably be related to your application. And if you were missing a key file needed to run your application, like a DLL, the name of the file will probably be in there in the details section
Go back to your installer and add that missing file. Or if that file is part of some support package (like, say, MS Redistributable for Visual C++) then add the installer for that too.

WPF: Image Path doesn't work after publishing

I have a trayicon in my WPF App. When I was debugging in Visual Studio, it works well:
System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon();
ni.Icon = new Icon("../../logo,ico");
But after I published it, the App threw an exception saying it could find the path of the image of the trayicon. I've search a lot resources on online, I saw many solutions use pack, but it seems pack only accept Uri variable and the path for trayicon must be string. I also tried to create a folder called Resource under the project solution and put the image file into it. This was the same: worked while debugging but would not work after publishing..
So based on #WPF Germany's suggestion, I solved the path issue after publishing the App by Clickonce. However, if I copy a App shortcut to system startup folder, it would find the ico in C:\WINDOWS\system32, which is not easy to workaround since systems32 usually requires admin right to access. Any idea for that?
Did you check, that your logo.ico file is copied to output path?
In VS you have multiple options to provide your Resource files.
First option:
Select your logo.ico file in solution explorer and choose None as Build Action and Copy always at Copy to Output Directory (at file properties).
After compiling you will find the file in a subdirectory of our OutputPath.
use (if logo.ico is placed in your projects root):
ni.Icon = new Icon("logo.ico");
other option:
Use Resource as Build Action and build your Icon using a Stream created from Resources.logo...

Why do I get a "Metafile not found" error when trying to compile a WPF application?

I had to rebuild a project in my application.
I created a new project, created each file, and copied the contents of each file into the new project one at a time making all the necessary changes.
Now when I try to compile the project, it gives me three errors:
The metafile c:\projects\project1\CustomerModule\bin\Debug\CustomerModule.dll could not be found.
Application Definition Element could not be given by the library project file.
The project file has invalid properties.
Clicking any of these does nothing.
What are these errors telling me? What do I need to change to get my application running again?
New info: If I do a "rebuild" on just the project, I only get the second two errors, so these seem to be the original issue, but what is the library project file and what properties are invalid?
I found the answer on this forum question.
Basically the thing I did wrong was copy images from a directory in one module to a directory in another module within the Solution Explorer. Don't do this. Instead, copy them from the Windows Explorer into the Solution Explorer, then everything works fine.
Click add a reference and browse for the dll file you want to use.
You may have to rebuild the dll for it to work.

Visual Studio crashes when I add a .settings file in a C++ Windows form application

I'm trying to add a .settings file in a windows form application by adding a (whatever) file in the project and have it named smthng.settings. Right after it is created, it crashes (if I look into the project's directory the file is there, but it's not "in" the project). Am I doing it wrong or could it be that the problem lies elsewhere?
Edit: It seems that it's the settings designer that crashes.
Partially Solved:If I add a (whatever).config file, then rename it to .settings and change it's file type to C/C++ Code (don't ask how I figured this out..) then I can add to it settings, but if I do add something that has any connection to the form then automatically a (whatever).config with thew same name spawns (which is identical to the (whatever).setting even if change one of them) and at the stdafx.cpp a #include '(whatever).h' appears, which is a problem, because there is not such a header. (if I erase it or just create a blank (whatever).h it doesn't work. Apparently I have to somehow connect all the data in the (w/e).settings to (w/e).h as well or maybe something else) Anyone had this problem before? Anyone has any ideas?
If it crashes, then regarding of what else you may be doing wrong, it's a bug - software shouldn't crash on invalid user input. That said, the causes could be anything from bug in VS itself, to bug in some third-party extension you're using, to system problems.
Make sure you have all the latest OS updates and service packs for VS2005. If it still repros then, report it as a bug to Microsoft Connect, and people responsible for this product area will take it from there.

Resources