C# How to get the User AppData folder, NOT AppData\Roaming? - c#

I used below code to get the user's AppData folder -
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
But what I got is "C:\Users\(users)\AppData\Roaming". Is there a way to only get "C:\Users\(users)\AppData"?

First of all, accessing that folder directly is probably not a good idea unless Microsoft has published an API to retrieve its location. This means that there are no guarantees that this folder will even exist.
If you for some reason really want to retrieve this folder, you could probably do something along the lines of
Directory.GetParent(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData))
Then to verify, you could also retrieve e.g.
Directory.GetParent(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData))
If the two are the same, it is likely the folder you want to find.
But again, it is probably a good idea to question the motivation on why you need this path in the first place.

Is this what you are looking for
first get the user name from Environment object.
string userName = Environment.UserName;
then, use that User Name for generating the path.
string path = $"C:\\Users\\{userName}\\AppData";

Related

How to store local directory path in database?

I'm working on restAPI project, I have to get list of directories paths, allow the user to choose one and save it in database. I created string variable and wanted to assign selected path to it, but when testing from postman I can't assign full path (e.g C:\dev\data) to string variable (receive bad string format). So I would like to know, what is the best way to store path in db, should I store it without C:\, and if so, how to take directory path without C:\?
The path "C:\dev\data" will give errors since backslashes are taken as escape sequences. If you need to store the whole path, you should replace the backslash with double backslash for it to work
"C:\\dev\\data\\name_of_file"
You can store this string in the database.
It's best however, to store the main root which in your case is C:\dev\data in a configuration file and just store the file name bit in the DB. To fetch the file from code, you read the folder root from the configuration file and just append the name of your file to it.
Hope this helps.
As far as I'm aware of, there is no such thing as limitation on what string you can save to certain cell in a DB (in terms of characters). It means, you must have made some mistake along the way.
Make sure first that you can pass chosen path to the back-end. (I assume it is Web-App you work on). Place breakpoint at the action in the controller and check if you receive the string in the first place.
Then step-by-step I'd suggest to move down the happy-path and make sure each step works as expected.
This way you'll easily locate your error.
Note: It feels like there is high chance of non-escaped characters in the string.
Make sure chars are escaped where applicable.

How to convert DOS path to normal path (.net)

I have a program that tracks changes on a local folder using a FileSystemWatcher object.
The issue is that sometimes, on some environments and situations (I do not know which ones), this watcher gives me an event on a DOS path ("/Hello/How/Are/You" becomes something like "/HE~1/HO~1/AR~1/YO~1").
What I am looking for is a way to force this path back into its full and normal aspect.
Or at least something that can tell me that the path is indeed a DOS path, so I can process the entry differently.
EDIT: it has to work on long paths (+260 chars), so Path.GetFullPath(sShortPath) does not work for me here!
Path.GetFullPath(#"/HE~1/HO~1/AR~1/YO~1") should do what you need.
The best method depends what you are looking for, if you just want to access the file once then the 8byte file names will work for internal file references
if you want to display to the user or store then there are 2 option
Path contains most of the tools you need to manipulate paths
fullPath = Path.GetFullPath(path1);
FileInfo and DirectoryInfo these 2 classes provide persistent access to files and directory information and while they can be created with any valid path both have a Full name property that provides access to the full path
As others said, Path.GetFullPath(sShortPath) works fine if not used on very long paths (+260 chars).
Here is a link I followed that worked for me.
GetLongPathName from kernel32.dll worked fine with me, I just had to change the 255 StringBuilder limit to a higher value to make it work with long paths.

C# Listing all directories in C:\

In my application every user can set his own save path to save his files and settings
so every time the user log in i must search a folder that contains the username+"Data"
for example if the user name was "Kim" i need to find the path to the folder KimData
when i try to get all directories in C:\ the UnauthorizedAcessException appears
so is there a way to search for that folder or just skip the unauthorized folders while searching ?
The UnauthorizedAccessExpection means that the caller does not have the required permission to access the directory/file. Since you're doing it locally, there are several options. After you attempt to copy the data from VS to the data in the file (Create, copy, delete) etc.... you can try File.SetAttributes(yourfile, FileAttributes.Normal).
You can also use Environment.GetFolderPath. Accordingly, this:
"Gets the path to the system special folder that is identified by the
specified enumeration, and uses a specified option for accessing
special folders."
Also, I'm guessing you're simply looking through the entire directory/folder/path all at once. A workaround would be to probe one directory at a time. This is assuming you are adding a file. Once you've found your directory, you can use:
Directory.GetFiles(path)
.ToList()
.ForEach(s => files.Add(s));
Directory.GetDirectories(path)
.ToList()
.ForEach(s => AddFiles(s, files));
EDIT: Some helpful related questions on stack to look at might be:
UnauthorizedAccessException
Directory.GetFiles
Take a look at Ignore folders/files when Directory.GetFiles() is denied access and see if it helps you.
However if you are trying to look for a specific folder - which according to your question the user can place anywhere - in the entire directory tree i advise against it as it probably will be slow.
I would recommend saving the path somewhere and reading it from there when the user logs back on.
If its a desktop app and each user runs the app with its own windows account an even better solution would be to always write the data to the user's ApplicationData folder which you can get with Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).
As the name indicates this folder exists to keep applications data and its individual for the user logged in to windows

full path of a folder in c#

i need to get the full folder path in a windows project using c#.I tried with path.getFulPath(filename).bt it returns the application path+filename.how can i get the actual path like "D:\eclipse_files\ads_data"?
A relative path such as myfile.txt is always resolved in relation to the current working directory.
In your case the current working directory seems to be D:\eclipse_files\ads_data so your relative file path gets resolved to D:\eclipse_files\ads_data\myfile.txt when you call Path.GetFullPath.
To solve the problem, either make sure that you start with an absolute path from the beginning, or, that your working directory is set correctly.
You can get/set the working directory using the Directory.GetCurrentDirectory and Directory.SetCurrentDirectory methods.
Your question is not very clear, but I think you're looking for this:
string path = Path.GetDirectoryName(filename);
If I have understood correctly, you have a filename, for example 'doc.txt', and you want to have a method to return the full path of this file regardless of where the application runs from?
If this is what you ask it is not possible. Have you considered that there might be several files called 'doc.txt' on your harddrives?
The best you can hope to do it to search all harddrives, and return a list of all files found with the same name, but that will just be ridicously slow.

How to determine a full path of a named folder?

I want to determine the full path of certain folders. In my array, I just have the names of the folders, but when my application will get installed on another user's machine, my program must be able to determine the full-path of these folders.
How to get the fullpath?
You can check the method Path.GetFullPath, it could be useful to what you're trying to do.
Path.GetFullPath Method
Did you mean the My Documents folder and the rest? It's not obvious for me from your question.
The My Documents folder is:
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
And the rest of the special system folders can be retrieved in a similar way.
By prefixing it with another Path. Which Path depends on your application.
string path = ...
string fullPath = System.IO.Path.Combine(path, folderNames[i]);
You could take a look at Environment.GetFolderPath(...)
Sounds to me that you mean actually the current path plus an additional path. I.e., suppose your application is installed in c:\installations and you need the relative path of resource\en-US, you want to find c:\installations\resource\en-US.
Normally I would go for getting the current path, but in Windows it is possible to start an application as if it is executing from a different path. A fool-proof way of getting the path of the current application (where it is installed) is as follows:
// gets the path of the current executing executable: your program
string path = Assembly.GetExecutingAssembly().Location;
// transform it into a real path...
FileInfo info = new FileInfo(path);
// ...to make it easier to retrieve the directory part
string currentPath = info.Directory.FullName;
Now it becomes trivial to get new paths.
string someRelativePath = #"reource\en-US";
string someFullPath = Path.Combine(currentPath, someRelativePath);
I know, it looks a bit contrived, but it is safer then using the current path.
FileInfo.FullName, if you are lucky and the constructor finds your file somehow (e.g. current working directory).

Categories