Access Problem with \bin\x86\Debug\AppX\Assets\ - c#

the situation is as follows:
I want to put pictures i get to a file in my assets.
I want to get the folder with:
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(Directory.GetCurrentDirectory());
and the create a file there with:
File.Create(folder.Path + #"\" + "Assets" + #"\" + storageItem.DisplayName + storageItem.FileType);
But it throws an System.UnauthorizedAccessException at me when i try to create the file. I need a permanent fix for this as other users (some without administrative powers) will need to use the program i am writing.
Thank you in advance

that's correct because you can not alter or create something in installed dir only option available to this dir is reading files that exist there.
if you want to keep some file in-app local or temp directory then you can use
AppliacationData.Currunt.LocalFolder/TemproryFolder etc to keep your file

Its likely your current directory is set to somewhere which required administrator permission. You can change your current directory:
https://learn.microsoft.com/en-us/dotnet/api/system.io.directory.getcurrentdirectory?view=netcore-3.1
The below looks like a fairly solid overview on where you should store things and has plenty of code examples
https://www.codeproject.com/Tips/370232/Where-should-I-store-my-data

Related

Universal Path for RecycleBin (Check when modified)

I'm trying to make a simple program that reads when the Recycle Bin was last modified the relevant code with follow:
DateTime lastModifiedRB = System.IO.File.GetLastWriteTime(#"C:\$Recycle.bin");
Console.WriteLIne("Recycle Bin was last modified at: " + lastModifiedRB.ToString();
There is an issue with this however as the directory: C:\$Recycle.Bin does not directly open the Recycle Bin itself so the reading is invalid.
Is there a universal path I can follow that would allow the program to read the actual date modified of the Recycle Bin?
Any help is appreciated, thanks in advance.
Fixed via adding
var user = WindowsIdentity.GetCurrent().User;
At start of program, if fetches the SID of the user which then I added onto the end of the directory:
C:\$Recycle.Bin\ + user

Exception: "Access to the path ... is denied"

I'm working on a program in C#, a part of which is to create a directory in the Application.StartupPath folder and then write a text file inside it using System.IO.File.WriteAllText(). My issue is that my program crashes, throwing an UnauthorizedAccessException and telling me that "Access to the path is denied", which is, well, odd, considering that it crashes regardless of the directory from which I am running the program, whether it be running from my cloud folders, Desktop, My Documents, etc, and even despite running it as Administrator in any of those directories.
The path from which I'm debugging it is C:\Users\Jeff\Google Drive\Documents\Visual Studio 2013\Projects\Palobo\Palobo\bin\Debug. It is using System.IO;, and the code I'm using includes:
Directory.CreateDirectory(Application.StartupPath);
File.WriteAllText(Application.StartupPath, "Password=" + x);
where x is some String data entered by the user.
The error I get is:
Access to the path 'C:\Users\Jeff\Google Drive\Documents\Visual Studio 2013\Projects\Palobo\mzdon29 is denied.
(mzdon29 being an encrypted result of jwalk96).
Does anyone have any ideas as to why I'm encountering this problem? Thanks!
Application.StartupPath is a folder (where your application is started from). Try to specify an exact filename inside that folder:
File.WriteAllText(Application.StartupPath + "\\MyFile.txt", "Password=" + x);
Let's look at this code:
Directory.CreateDirectory(Application.StartupPath);
File.WriteAllText(Application.StartupPath, "Password=" + x);
You're trying to create a directory that already exists, and then you're trying use the directory as a file name! You need to add something to end of the path, so that you're working with a new folder and file.
Also, using the StartupPath for this is poor practice in the first place. You can create a shortcut that sets the startup path to anywhere. But specifically, it's common for the default StartupPath to be somewhere under the Program Files folder. Items under this folder are read only to standard users by default. Instead, you should look at using the Application Data folder, like so:
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Finally, this sure looks like it's saving a password in plain-text. Do I really need to go over how bad that is? You shouldn't even save passwords encrypted (hashing is different than encryption), and this is one of those things that's so important you shouldn't even do it for testing/learning/proof of concept code.

Environment.SpecialFolder.ApplicationData returns the wrong folder

I have a strange problem: my .NET 4.0 WPF application is saving data to the ApplicationData folder.
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\myProgram\\";
99.9% of the cases are working great, but on some computers it returns the wrong folder - instead of returning the user folder it returns another folder:
C:\Users\<user>\AppData\Roaming\myProgram\ --correct
C:\Users\s\AppData\Roaming\myProgram\ --wrong
The wrong folder has no write/read permission so my program doesn't work.
It seems the program is running under a different user, but if I check the Task Manager the user is the logged one.
The problem seems to be occurring with domain users with few permissions.
Do you also create a text file to write?
If so save a file such as:
String path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var filePath = Path.Combine(path, "filetowrite.log"); // Handles whether there is a `\` or not.
if (File.Exists(filePath))
{
......................
}
Note also before doing any file operations, one should check if directory exists.

Renaming a file on a remote file server in C# / Python

I need to rename a whole heap of files on a Windows file server - I don't mind what language I use really as long it's quick and easy!
I know it's basic but just to clarify - in pseudo-code...
server = login (fileserver, creds)
foreach (file in server.navigateToDir(dir))
rename(file)
I know how to do this in Python/C# if I was a local user but have no idea if it's even possible to do this remotely using Python. I've searched for code snippets/help but have found none yet.
Thanks.
Use \\servername\sharename\somefile.foo for filenames - provided you have access to connect to it and are running on windows.
You could also map up a network drive and treat it as any other local drive (y:\sharename\somefile.foo)
You could also use PSEXEC to execute the code remotely on the server if you need the performance of locally executed code. See http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx
Have a look at pyfilesytem, it provides a consistent interface for local and remote filesystems.
The following renames a file in each of the sub-directories of the folder path given. It renames the file from the given filename (eg."blah.txt") to foldername+extension.
NB. Z can be either a local or network drive (ie. if folder is on file server map network drive to it).
For example from a shell...
python renamer.py "Z:\\FolderCollectionInHere" blah.txt csv
... will rename a file 'blah.txt' in each immediate sub-directory of "Z:\FolderCollectionHere" to .csv.
import os
import sys
class Renamer:
def start(self, args):
os.chdir(args[1])
dirs = os.listdir(".")
for dir in dirs:
try:
os.rename(dir + "\\" + args[2], dir + "\\" + dir + "." + args[3])
print "Renamed file in directory: " + dir
except Exception:
print "Couldn't find file to rename in directory: " + dir
Renamer().start(sys.argv)

Saving session data to ApplicationData Folder (Windows 7/WindowsVista/WindowsXP)

I am trying to save session data to the users local ApplicationData folder, but it Windows just seems to create a new ApplicationData folder with the files inside it wherever it wants. Sometimes it ends up on my desktop, and sometimes it's elsewhere. (like the bin folder, for example).
It doesn't make any sense.
I know that it redirects due to insufficient permissions etc but this is just horrible.
Can somebody please tell me if this is the right way to save some text file info to my applications AppData folder?
File.WriteAllText(
Environment.SpecialFolder.ApplicationData +
"\\MyApplicationNameFolder\\" +
filename + ".txt");
Environment.SpecialFolder is an enumeration representing the constants you need to use when requesting the path. It doesn't give you the path.
Use GetFolderPath with that enumeration value to get the path.

Categories

Resources