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.
Related
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";
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.
I have an application that accepts user input and does stuff to files. The users select a file and it might move it, delete it, rename it, ftp it etc. The application uses a hash table to store recently used files and their paths.
The main problem I am looking into now is one of the add-ins is saving the path incorrectly, it is saving it as such: C:\David\\File.txt
The part of the application that deals with file io tries to ensure the file exists prior to doing stuff with a File.Exists(path) call. This call is returning true even for the above example. Can anyone explain why this might be?
The issue I am facing is, beyond one module saving the path incorrectly, certain modules that interact with the file are accepting that incorrect path and working fine while others see it and crash. Although currently I am going to fix this by getting the path saved correctly, I'd like to understand what is going on here.
You have a false premise: that C:\David\\File.txt is an invalid path. Multiple backslashes are accepted fine in Windows. Try notepad C:\David\\File.txt in a command prompt as an experiment--it should work.
For more info, see this other SO q/a that reaffirms this. Any number of backslashes are fine, and this can be used as a "easy" way to combine paths without worrying about the number of backslashes. For example, the user can provide C:\David or C:\David\ and you can add \test.txt without worrying which input the user provided. However, Path.Combine is the real way to do this in C#.
Edit: To remove your extra \'s easily before passing the path into the other program, try splitting the path into the drive and folder names and combining it back together into a path. Like this:
string path = Path.Combine(pathWithManyBackslashes.Split('\\'));
Because Split doesn't create new entries when the delimiter repeats, you get rid of them. For example, C:\David\\File.txt => [C:, David, File.txt].
I'm creating a program that controls if words in a textbox are present in a text file.
The question is: Is there a way to find the path of that file without depending on the computer you're on?
If you don't use an absolute value for the path - it'll always try to resolve it relative to where your application is.
So if you put the file in the same folder as your application, it'll find it.
Otherwise, if you want your user to locate the file for you, you could use an OpenFileDialogExample here
Another option is to use one of the 'known' paths (such as My Documents). You can do this using Environment.GetFolder
But all of these depend on what you're trying to do exactly.
You can use relative paths. For example, if your path is "file.txt" and save the file, it will be saved next to your exe. Same thing for opening.
I have a drive on my computer that has folder--some of which have lots of folders w/in other folders and also contain files.
I need to migrate these docs into sharepoint, however a LOT of these folders and files have invalid characters that will not allow me to migrate into sharepoint (i.e. " / \ &, etc.)
Is there any way to write something in C# that basically removes these invalid characters from all folders and files?
Please help!
Yes. A way to do this would be to drill through the directory structure recursively, and for each file name, check if it is valid, and if it is not valid, make a valid filename, and upload to Sharepoint.
You can make a Regex that matches all disallowed characters, and replace them with an allowed character, such as underscore. If you need names to be unique, and you are worried you might create duplicate names with this approach, store all names that has been used (ie. uploaded to Sharepoint) in something like a HashSet, and check that before using the generated name. If the name already exists, you can add a pre- or suffix, flag for human intervention, or do something else, depending on your requirements.