I am trying to check if a file exists in a specific folder on the local machine. The file name is created by concatenation of the first and last names. I am not sure how to pass the file name into the file.exists since the file name changes each time? I am using the following statement to check the folder.
Code:
if(File.Exists(#"C:\TestDocuments\filename.xml"))
{
MessageBox.Show("The File Already Exists");
}
Path.Combine(String, String) concatenate the two strings, with an intervening separator character. I think here you need is string.format may be like
if(File.Exists(string.Format(#"C:\TestDocuments\{0}{1}.xml",firstName,lastName))
Kevin, as #Dai said you will want to use Path.Combine.
Your code afterwards might look something like this:
if(File.Exists(Path.Combine(directoryPath, filePath)))
{
MessageBox.Show("The File Already Exists");
}
This will combine a say "C:\TestDocuments" with "filename.xml" and can be used for other file names too.
You could essentially do something like this:
public static string CheckFileName(string name)
{
if(string.IsNullOrEmpty(name))
throw new ArgumentNullException();
int i = 0;
string file = Path.Combine(ConfigurationManager.AppSettings[#"FolderPath"], name);
while(File.Exist(file))
file = String.Format(file-{0}, i++);
return file;
}
That is an incredibly rough implementation, as it doesn't handle file extensions and or null values. But if the file exist it should auto increment until a file name that is free exist. This will alleviate files being overridden, but it all depends on your goal.
The above Does:
Pulls the path from your app.config, which would allow you to encrypt it.
Auto increments
Validates that the parameter isn't null.
Since it is a method, it is reusable.
The above Doesn't
Handle extensions in the file name.
Remove previous file, there isn't any overwriting.
When dealing with file paths, you often need Path.Combine to avoid any errors in slashes. That is where String.Format can run into issues, unless a separator exist. The Microsoft Developer Network has some great information on this.
You should check System.IO.
Related
This is the simple C# code I wrote :
string file = #"D:\test(2021/02/10).docx";
var fileName = Path.GetFileNameWithoutExtension(file);
Console.WriteLine(fileName);
I thought I would get the string "test(2021/02/10)" , but I got this result "10)".
How can I solve such a problem?
I just wonder why would you want such behavior. On windows slashes are treated as separator between directory and subdirectory (or file).
So, basically you are not able to create such file name.
And since slashes are treated as described, it is very natural that method implementation just checks what's after last slash and extracts just filename.
If you are interested on how the method is implemented take a look at source code
I have a problem.
I save json from web, in json files on my computer, and the name of this json file, is the web adress of the json.
For that, I get the web json into string, and then I append it in a file, with File.AppendAllText(path, content)
After some time, i also need to read json from this file with File.ReadAllText(path)
My problem is sometimes, two json have a very similar name, for example :
*com/doc/BACr and
*com/doc/BAcr
Problem, the path given in the methods of the class File are note case sensitive, and I end writing twice in the same file, corrupting it.
I've found on the internet solutions for the same problem for the method File.Exists(path), but nothing to replace the methods I use to read or write.
Any of you know a setting, or even another method that would be case sensitive on the path ?
Thank you
Edit : I'm obviously working on windows :(
Edit bis : I can't change the filename, because in some others json, there is reference to web path, and when I play again my local jsons, if the filename is modified, it won't be found. It's the reason I need both write and read with case sensitive path.
You need something that makes your files unique and in the same time something that allows you to rebuild this uniqueness when you want to read back these files.
Suppose that your couple of files is named "BAcr" and "BACr". You can get the HashCode of these two strings and you will get two different values
string file1 = "BAcr";
int file1Hash = file1.GetHashCode(); //742971449
string file2 = "BACr";
int file1Hash = file2.GetHashCode(); //-681949991
Now if you concatenate this hashcode to your filename you will get two different files and you will be able to recalculate the same hashcode for the same input filename
string newFile1 = $"{file1}.{file1Hash}";
string newFile2 = $"{file2}.{file2Hash}";
you will save your data in these two recalculated filenames and when you need to reload them you use the same trick to get the filename used to save the data starting from the same input "BAcr" or "BACr".
But string.GetHashCode doesn't guarantee uniqueness in its results so, still using the same general idea Jeroen Mostert uses this method to get an unique code from the input value
string unique1 = string.Join("", file1.Select(c => char.IsUpper(c) ? "1" : "0"))
string newFileName1 = $"{file1}.{unique1}";
Windows paths are indeed case insensitive, so you cannot have these filenames.
One solution would be to change the filename if it already exists...
For example;
if (File.Exists(fileNameToSaveTo)){
// Note: Your example file names did not have an extension,
// but if they do, you will need to first extract that then add it back on
fileNameToSaveTo = fileNameToSaveTo + "1";
}
If using this solution, you would have to also update whatever identifier your program uses to read back from the file at a later date... as you have not posted any code I cannot guess as to what form this takes, but hopefully you get the idea?
Edit:
Upon re-reading your question... it appears you use AppendAllText... In this case, this should not 'corrupt' the file as you suggest, but should simply add the contents to the end of the file? Is this not what you observe?
Edit2:
After reading comments Iomed - you could use Convert.ToBase64String on the filename in your write before writing the file, the use Convert.FromBase64String on the filename in your read function before reading the file. This will allow the filename to be different based on the capitalization.
Another alternative would be to parse the JSON (the new one AND the existing file) and add the objects to an array, then write that to the file instead, avoiding your 'corruption' issue?
given paths: PathA, patha
for two files, use base64 trick:
string PathToFile(string url) => System.Convert.ToBase64String(Encoding.UTF8.GetBytes(url));
so:
Console.WriteLine(PathToFile("pathA")); //cGF0aEE=
Console.WriteLine(PathToFile("patha")); //cGF0aGE=
In my application I build a static string when a user uploads or downloads a file. In that string the filename is passed from the frontend in that string. In this way the user could do things like ..\..\another file.file to tamper and get data from other users. Therefor I need to filter the filename that I get to prevent this. What are the characters that need to be filtered to prevent tampering? I now have the double dot and the back and forward slashes. Is there anything else I should take into consideration? Is there maybe a standard way to do this in C#?
I would suggest using Path.GetInvalidFileNameChars:
public static bool IsValidFileName(string fileName)
{
return fileName.IndexOfAny(Path.GetInvalidFileNameChars()) == -1;
}
.. is typically only dangerous when preceded and/or succeeded by a \ or /, both of which are included in the array returned by GetInvalidFileNameChars. By itself, .. is harmless (unless you’re specifically resolving directory paths), and you shouldn’t forbid it since people might want to introduce ellipses in their filename (e.g. The A...Z of Programming.pdf).
What if different users save a file with the same name? Are you creating a folder for each user?
Most likely what you should be doing is storing the name they provide in a database record, which also contains a pointer to the actual file (which uses a file name which you generate, perhaps a guid). You could also consider using the filestream data type if you'd like to save the document in the database as well.
Nothing good can come from letting your users determine file names on your server :)
I get a string that more or less looks like this:
"C:\\bláh\\bleh"
I make a FileInfo with it, but when I check for its existence it returns false:
var file = new FileInfo(path);
file.Exists;
If I manually rename the path to
"C:\\blah\\bleh"
at debug time and ensure that blah exists with a bleh inside it, then file.Exists starts returning true. So I believe the problem is the non-ascii character.
The actual string is built by my program. One part comes from the AppDomain of the application, which is the part that contains the "á", the other part comes, in a way, from the user. Both parts are put together by Path.Combine. I confirmed the validity of the resulting string in two ways: copying it from the error my program generates, which includes the path, into explorer opens the file just fine. Looking at that string at the debugger, it looks correctly escaped, in that \ are written as \. The "á" is printed literarily by the debugger.
How should I process a string so that even if it has non-ascii characters it turns out to be a valid path?
Here is a method that will handle diacritics in filenames. The success of the File.Exists method depends on how your system stores the filename.
public bool FileExists(string sPath)
{
//Checking for composed and decomposed is to handle diacritics in filenames.
var pathComposed = sPath.Normalize(NormalizationForm.FormC);
if (File.Exists(pathComposed))
return true;
//We really need to check both possibilities.
var pathDecomposed = sPath.Normalize(NormalizationForm.FormD);
if (File.Exists(pathDecomposed))
return true;
return false;
}
try this
string sourceFile = #"C:\bláh\bleh";
if (File.Exists(sourceFile))
{
Console.WriteLine("file exist.");
}
else
{
Console.WriteLine("file does not exist.");
}
Note : The Exists method should not be used for path validation, this method merely checks if the file specified in path exists. Passing an invalid path to Exists returns false.
For path validation you can use Directory.Exists.
I have just manuall created a bláh folder containing a bleh file, and with that in place, this code prints True as expected:
using System;
using System.IO;
namespace ConsoleApplication72
{
class Program
{
static void Main(string[] args)
{
string filename = "c:\\bláh\\bleh";
FileInfo fi = new FileInfo(filename);
Console.WriteLine(fi.Exists);
Console.ReadLine();
}
}
}
I would suggest checking the source of your string - in particular, although your 3k rep speaks against this being the problem, remember that expressing a backslash as \\ is an artifact of C# syntax, and you want to make sure your string actually contains only single \s.
Referring to #adatapost's reply, the list of invalid file name characters (gleaned from System.IO.Path.GetInvalidFileNameChars() in fact doesn't contain normal characters with diacritics.
It looks like the question you're really asking is, "How do I remove diacritics from a string (or in this case, file path)?".
Or maybe you aren't asking this question, and you genuinely want to find a file with name:
c:\blòh\bleh
(or something similar). In that case, you then need to try to open a file with the same name, and not c:\bloh\bleh.
Look like the "bleh" in the path is a directory, not a file. To check if the folder exist use Directory.Exists method.
The problem was: the program didn't have enough permissions to access that file. Fixing the permissions fixed the problem. It seems that when I didn't my experiment I somehow managed to reproduce the permission problem, possibly by creating the folder without the non-ascii character by hand and copying the other one.
Oh... so embarrassing.
What's the best way to get a string containing a folder name that I can be certain does not exist? That is, if I call DirectoryInfo.Exists for the given path, it should return false.
EDIT: The reason behind it is I am writing a test for an error checker, the error checker tests whether the path exists, so I wondered aloud on the best way to get a path that doesn't exist.
Name it after a GUID - just take out the illegal characters.
There isn't really any way to do precisely what you way you want to do. If you think about it, you see that even after the call to DirectoryInfo.Exists has returned false, some other program could have gone ahead and created the directory - this is a race condition.
The normal method to handle this is to create a new temporary directory - if the creation succeeds, then you know it hadn't existed before you created it.
Well, without creating the directory, all you can be sure of is that it didn't exist a few microseconds ago. Here's one approach that might be close enough:
string path = Path.GetTempFileName();
File.Delete(path);
Directory.CreateDirectory(path);
Note that this creates the directory to block against thread races (i.e. another process/thread stealing the directory from under you).
What I ended up using:
using System.IO;
string path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
(Also, it doesn't seem like you need to strip out chars from a guid - they generate legal filenames)
Well, one good bet will be to concatenate strings like the user name, today's date, and time down to the millisecond.
I'm curious though: Why would you want to do this? What should it be for?
Is this to create a temporary folder or something? I often use Guid.NewGuid to get a string to use for the folder name that you want to be sure does not already exist.
I think you can be close enough:
string directoryName = Guid.NewGuid.ToSrtring();
Since Guid's are fairly random you should not get 2 dirs the same.
Using a freshly generated GUID within a namespace that is also somewhat unique (for example, the name of your application/product) should get you what you want. For example, the following code is extremely unlikely to fail:
string ParentPath = System.IO.Path.Combine(Environment.GetEnvironmentVariable("TEMP"), "MyAppName");
string UniquePath = System.IO.Path.Combine(ParentPath, Guid.NewGuid().ToString());
System.IO.Directory.CreateDirectory(UniquePath);