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.
Related
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
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.
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.
How can I get the path of a file on my computer or on the local area network.
I would to show a window that allows me to browse the file system when I click a button, and I want to select a file and get the path to the file. How can I do this?
P.S. I'm not looking to upload the file; I just want to get the path.
The web application is running on the server, and you don't have access to the client file system at all. Can you imagine how much of a vulnerability that would be? I know I don't want the sites I visit inspecting my file system...
EDIT: Question is for a WinForms application
For a WinForms application, you can use the OpenFileDialog, and extract the path with something like this:
If you're looking for the file path:
string path = OpenFileDialog1.FileName; //output = c:\folder\file.txt
If you're looking for the directory path:
string path = Path.GetDirectoryName(OpenFileDialog1.FileName); //output = c:\folder
In general, the System.IO.Path class has a lot of useful features for retrieving and manipulating path information.
To do this in VB.NET use the FolderBrowserDialog.
Due to security restrictions browsers include for user safety, you can't manipulate the client file system directly. You can only use to let them pick a file, and even then it only sends the filename, not the whole path.
The FileSystem API in HTML5 allows for file manipulation, but only within a sandbox for your site specifically, not browsing across the network or other files on the client system.
Instead, provide your users easy steps on how they should use My Computer (or whatever equivalent on other OS's) to navigate to the file and copy & paste the path into a simple text input box.
Have you taken a look at the Path class from System.IO ?
How to locate the path of other drive's folder in server?
For ex:
The application is there in 'C:\SomeFolder\', now I want to export a file to the drive 'D:\AnotherFolder\' on the server. How can I achieve this?
First of all you need to have permissions to access the folder. The user that needs to be able to access the folder is the same user as is running IIS. Usualy it is a built in account IIS_* that is running the instance.
As long as this user has access to the folder, you can just use File Read / Write as you normally do.
Read this article about C# File Handling.
Example - Search for files on another drive
var files = Directory.GetFiles("D:\\", "*.txt");
This will give you an array of files with the extension .txt found on D:\.
Also read this: How to: Copy, Delete, and Move Files and Folders (C# Programming Guide)
If you mean to get a list of all drives known on a system you can use Environment.GetLogicalDrives.