Update an assets file (C#/UWP) - c#

I've got a csv file stored in my solutions's assets folder. Every now and then a copy of the csv is updated on a shared drive. Is there a way to copy this file and replace the version in the assets folder automatically. (I'd just reference the shared file rather than an internal one but occasionally the network plays up. I guess I could just have it copy to a location locally but I'd rather do it this way if possible)
I've added something to copy the file over on app launch but don't have permissions. I've guessing that assets are read only during runtime? Is there a way around this?
This is the line of code that triggers the error. File.Copy(sharedPath, assetPath, true);
Error: Access to the path 'C:\Users...AppX\Assets\file.csv' is
denied.

Update an assets file (C#/UWP)
During checking the path, and it looks a apps installation folder, unfortunately, the installation folder is read-only, we can't write data into. we suggest you use app' local folder that with full permission.
For your scenario, you could copy the csv file where in the assets folder to app's local folder when app fist launch. when you want to update csv file you just need to replace local folder's csv with shared drive.
For more detail please refer this document.

Related

How should i do to reference one file in my project, so when i publish the application, the file is generated in a specific folder?

the problem that i have with my application is about how to reference some files, when i load and image, etc, i use Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "..\\..\\..\\")); and this works fine for debugging and for some files, but doesn't work for some essential files, credentials.txt, config.ini, this files are searched in appdata when i run the published file and i don't know how i should reference them.
I try to generate some initial files so i am sure where they are and that they exists, but for folders that works great with
System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().Location);
with that i can make the folders where the app is run but you can't generate a file without load the content of the original file, and i don't know how to reference them.
Example:
Project.
Root of the project.
utils/key/credentials.txt
When the published file is executed i want to generate and store credentials.txt like this:
Executable (app.exe stored for example in "my documents")
"my documents"/utils/key/credentials.txt
how i do that? when i run the published application you just have the .exe, the dlls, and the resources are embebbed, so the uri doesn't works.
Assuming you have sufficient permissions, you could create a folder in the output folder of your compiled .exe at runtime using the Directory.CreateDirectory method.
If the utils/key/credentials.txt file is part of your deployment, you should set its Build Action to Content and the Copy to Output Directory property to Copy if newer in Visual Studio. This will add the utils and key folders to the output directory of the .exe, which is typically c:<project-folder\bin\Debug or \Release when you build from Visual Studio.
You can get the absolute path of the output directory like this:
string path = System.IO.Path.GetDirectoryName(
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
Depending on your requirements, you may then append the relative path of the file within your app to the absolute path.

C# get relative path of resources folder

I want to acess a .txt file, which I stored in the resources folder of my project, there where all the imported pictures are stored as well.
I want to use something like a relative path, since every user would have safed his programm somewehere else on his Pc, but there is always the resources folder at the same place, compared to the programm folder.
I tried to used this: displayText = System.IO.File.ReadAllText("Resources\\startmessages.txt"); but this isn't working.
I get this error message: System.IO.DirectoryNotFoundException:, it lists the unrelative path to the .txt there as well, so I don't get, why it cant read it.
Thanks for your Help.
What #ChetanRanpariya is trying to tell you is, that your programm is built in another folder than your folder Resources is sitting to. So you have explictly tell your file Resources\startmessages.txt to copy itself on build process, so it get copied to said another folder. Assuming that you are using Visual Studio, you have to right click on your file and set Copy To Output Directory to true. Its relative folder path (Resources\) will be taken over. You find your build folder somewhere in your bin folder depending on configuration and framework. :)
Current Path where your executable is
Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).FullName
Path to Solution
If you are using Visual Studio and need to access the folders in the solution directory, you can use .Parent method,
Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.FullName
Use of Path.Combine
and once you have the location of your Resource folder, use Path.Combine to get the location to read files / content etc
Path.Combine(Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.FullName, "Resources\\startMessages.txt")

I want to access resources in uwp

Firstly, sorry this beginner's question.
But i'm really don't know the concept of path
I want to save my image files on uwp's resources History folder that i created.
So that I can open that images or save them.
But i can't find a way how to access my own folder
I tried appdata.localfolder but that's not for me
Anyone know the solution to this?
Thank you for reading this !
The images in the History folder are part of the program and are built to the package's installation directory. You can take it the equivalent of the C:\Program Files\Your App folder, the files are read-only.
To access the files from the History folder, use
var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///History/yourfile.jpg"));
AppData.LocalFolder on the other hand, is your app's data folder, that is where the app data files - files that generated by the app - are located, and you can save app data there.
You can refer to this for how to write to the app data folder.

How to Use a Windows App with Embeded files and folders to copy to a destination?

I am trying to write a small application, whose only purpose is to copy some folders and .cs source files into a user specified Directory, I can do it easy enough by simply having the application look for the files and folders in its own install directory then copy them to thier destination Directory, but I was wondering if its possible to Embed the Folders and Files into the Application, so that when you run the application it creates or copies the folders and files from the exe app directly to the install directory, rather than searching for them in the apps install directory then copying them over. Basically Im trying to only have a single exe file rather than having an exe file and a bunch of folders and files along side it.
Is this possible to do with just a Windows Form App without using an actual Installer Class?
Yes. Embed the files into the application executable as embedded resources. Then when your application runs, access the embedded files and write them to disk in the desired directory structure.
Here is an example of how to embed and access embedded resources from your application assembly.
http://support.microsoft.com/kb/319292
Sure you can, use the BuildAction property as Content or Resource.
Depending on the number and structure of files/folders, you may also consider embedding one zip file and extracting it with sharpziplib or some such.

Deploying c# application issues

I have a application with one folder which i added by right clicking the project, selecting add folder. Inside this folder i have xml files which are set to build action:content, copy to output directory: copy if newer (i have tried setting to embedded resource) As well as this i have a few text files and so on.
In my bin/debug output directory i have the exe, the folder with the xml, the stand alone .txt files and so on. My problem is, if i send the exe to my friend to try he always gets an exception thrown.
Say he puts the exe on the desktop, my programme at some point reads the filenames of the xml files in the folder. It uses the following code to do so
String[] filePaths = Directory.GetFiles(#"DataSources\");
I assume that because of this, when the exe runs from the desktop, it expect the folder of .xml files to be in the same place? I have the same type of exception when trying to read the .txt files too. What am i doing wrong here?
Thanks for your time
When reading from files using relative paths you get the one relative to the applications current directory. tip: In C# you can see what directory that is using Environment.CurrentDirectory.
So if you create a shortcut on your desktop, you need to make sure you right click the shortcut and set its "Start in"-folder to the directory of your application. That way its current directory will be set when its started and relative paths will be relative to that path and not the path of the shortcut.
If you actually moved the exe file to the desktop you also need to move any resources that it needs, so if it wants a folder named "datasources" you would have to move that folder as well, or set the current directory when you start the application.
Have you tried something like: http://msdn.microsoft.com/en-us/library/system.windows.forms.application.executablepath.aspx or http://msdn.microsoft.com/en-us/library/system.environment.currentdirectory.aspx ?
So
Directory.GetFiles(environment.currentdirectory + #"\DataSources\");

Categories