I am currently using NSIS to create an installer for my app. My app is made using C# (with Unity3D) and I was wondering if there is an equivalent to the two following functions in C# that I am using to save files (save data and such) during the execution of my app, in Python.
I want to know the paths to the save files to be able to remove them in my uninstaller !
Here are the paths :
Save path :
Application.persistentDataPath
Image path (the user is able to save some pictures in his picture folder) :
Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)
Is there a way to get these paths in Python ? Or in my .nsh file (where the uninstall files are listed) ?
The NSIS shell constants are listed in the help file (4.2.3 Constants). You are probably looking for $APPDATA\YourApp and $PICTURES...
Related
I'm working with Avalonia to create a MacOS desktop program using c#. I can run the program locally or output it to a final app file using dotnet.bundle. Attached image shows the contents of the app file using Mac's "show package contents".
There are files that are deployed to the app bundle that I would like to read when running the app, but don't know how to reference them. Since this is in C#, I can't find any docs similar to what apple provides here https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFBundles/AccessingaBundlesContents/AccessingaBundlesContents.html#//apple_ref/doc/uid/10000123i-CH104-SW1
I've tried referring to the parent directory, but this only works in design time.
I've tried a variety of syntax including Environment.GetCurrentDirectory as prefix to the below path.
string path = "Repository/CreateTables.sql";
string assets = File.ReadAllText(path);
Thanks
Thanks for the tip, yes I have been trying to print out paths but oftentimes they were blank when run from the .app (for example, Environment.CurrentDirectory returns ""). I did find a way to get at the path here https://github.com/AvaloniaUI/Avalonia/issues/5443#issuecomment-775906396, and it was not the path I was expecting. I thought the location of the file would start at myapp.app, but you need the path to myapp.app as well.
/Applications/MyApp.app/Contents/MacOS/Repository/CreateTables.sql
Say I use some .json files to descript some object data which effect to the program's behavior, I hope to use these files in the following scenarios
The default values, for this purpose, I need a set of files follows with the application to be packed and installed.
I wish it could be edited by human manually. (Because something have no interface to be modify on UI)
Both user and the program need to kwnow the location the files will be placed after installation.
In debugging stage, I could put these files in the user\AppData\Local.. folder and I know how to access them, but I don't know how to put files into the package and will them generated to anywhere after install?
Thank you for any suggestion.
ps.
I use the "Blank App (WinUI 3 in UWP)" template to create my
application.
I'm new in UWP and WinUI, I used to write traditional Windows Form programs.
How to include externel user files into UWP side-loading package?
You could place the json file into app's project and set the file property as Content, then it will deploy into installtion folder after package install. and please note the json file is readonly in the installtion folder.
so you could call CopyAsync method copy the file to the destination folder that app's local folder with full permission.
For more details about file access permissions please refer this document.
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.
I have designed and created two seperate windows application ( say winapp1 and winapp2). Winapp1 creates a file(say file.txt) in the disk containing some text. And I need to merge this file.txt with precompiled version of winapp2 ie winapp2.exe so that the winapp2 can display it.
Winapp1 creates a file with different contents each execution time. And for each file there will be a copy of winapp2 that needs to be merged.
Is there a way to this? If there is, help me.
And my English is not so good. Thanks for reading this.
The easiest solution, (assuming that releasing the source code for winapp2 to the system that runs winapp1 doesn't present problems) is to have the entire source for the winapp2 project accessible to winapp1. The source should include a blank copy of the file as an embedded resource. Winapp1 can then modify the source file and use MSBuild to build a fresh copy of Winapp2.
To modify the text file, you should be able to just use the normal file manipulation .NET methods and to call MSBuild, you may be able to do it via a .NET class, but you can certainly just call the msbuild executable from the relevant .NET framework folder in %windir%\Microsoft.NET\Framework\ and pass in the full path of the project file.
I have created a File/folder copy utility in C#.net. its working fine. problem is that when i right click on folder than my program's name appear their and its also working fine. but when i right click on any file or group of files and folder. so tell me which and where i need to create a registry key through my program so i can do that.
Not sure I understand the question, but here are some links that might help:
Registering File Types:
How to associate a file extension to the current executable in C#
Filetype association with application (C#)
Windows Shell Integration:
Windows Shell Integration using .NET
http://www.codeproject.com/KB/cs/dateparser.aspx