So I can`t really say where it came from or what to do about it. But I can at least talk a little bit about what it means to have a legal form. This, of course, is done through an exception, because the path is an illegal means for the class. It should be a backslash “ and not a forward `/` Hello.I get the following error: “The path is not of a legal form” in C# in the code For me, it seems that the current line has invalid path characters or something like that. A “path” is initially the string that indicates where a file is located on your computer. There is some kind of mistake (like an illegal character or it can be something else) that makes everything that tries to deal with this path abandon; to say, “You know what? I can`t do with it what I have to do because it has an illegal form. It`s not fair. There is something wrong with this specification of a file. When you read the file paths in the list box, first make sure that the full path to the file is first displayed in a correct format, for example: When I start, the message “The path has no legal form” appears. And sometimes when browsing, this is also displayed.

What does that mean? Open the text file, does the path look correct? (publish a single line) For example, if you have a file in a folder named My Documents at the root of your C drive and the document name is document.doc, the full path is C:/MyDocuments/document.doc. This happens when I just started the automation of main.xaml And it worked How exactly is it? Now, what happens in a case like this (“This path is not a legal form”) is that there is some kind of error in the path specified by an application`s configuration setting. @Kakooza-Allan-Klaus It could be that an invalid character occurs on your way. Use the following link to learn more: Run the app again, let it read the text file – does it read it? (assuming the path seems correct) This gives all the information to find that file on your hard drive: Well, the path/file you gave an example of looks fine. 3 – I tried Trim(),TrimEnd(),TrimStart() and it doesn`t work. Error: “The path has no legal form” Well, of course (what I just described) has what I consider a “legal form”. Save Listbox items (contains the path and file name) Unfortunately, I can`t tell you what, because it really depends on the origin of the message (which is often in the title bar of the message box) or the actual path it complains about. For example, he may include the path (which, according to him, has no legal form) in the message complaining that the path has no legal form. It may also be that there is an entry somewhere that is an invalid entry that causes it to lift the exception. But that`s essentially what it`s all about. He complains about a file name that he can`t understand where the file is. In your foreach loop, first check the length of the line before continuing with the code: The problem is that there are characters that are not allowed to be used in file and folder names, or they are more difficult to use in file and folder names and cause this type of message if misused.

Also, looking at your example – you said that “it stores something like this:C:/music.mp3” Are you able to get a list of every file it reads and display it here for our benefit and yours? In this way we can see on which line the error occurs and which entries are in the text file / table of rows Just a wild guess, but maybe you need to set EnableRaisingEvents to false before changing IncludeSubdirectories and the path? Thus:. As you said, when the application is closed, the application rewrites the files to the text file – right? The first 2 lines you posted – are they from the file itself or from what goes through the foreach loop? Well, what`s going on? If this happens at startup and it happens occasionally, the only way to know where it came from is to try to identify the program that is displaying the error message at the time of display. 2 – I replace SystemInfo(line) with MessageBox.Show(line) to see what`s in this line, and everything looks fine. I run Kubuntu and Win 7 in a dual-boot configuration. A few years ago, I created a file called “Earth: Where We Live”, which is a perfectly valid file name in a Unix-like file system. I saved it to an NTFS partition that I use for work created in both operating systems. The problem is that when NTFS runs on Windows, it chokes on a colon used in a file name. Unix-like operating systems have no problems with the colon. As a result, Linux was able to open the file properly, but Windows couldn`t even see it. It was completely invisible. After understanding what was causing the problem and renaming the file to get rid of the colon (with Kubuntu), the file appeared in Windows.

As it appears at startup and sometimes with the browser, I would start with QuickTime, Java and Adobe Reader. These are often loaded at startup and used by some websites. For example, it would be difficult to create a file with a vertical bar in the file name. You would have trouble creating a file whose file name contains a greater than sign or a less than sign. As suggested, can you please write here what each line currently reads in the foreach loop and also an example of the list.txt? The author did not identify the operating system, but if it is Windows, it can help. At what part/distance from the foreach loop is the exception thrown? What was the value of the variable in the foreach loop that caused the exception to be thrown? This means that something is being misconfigured on your computer. If you tried this, you would receive an error message. But that`s the kind of thing that happens. There is a problem with a file name specified as a setting as a configuration option for a program that you run at startup and then occasionally while browsing. To be honest, to be fair, it may not be something you set up. It can be some sort of ill-defined configuration when configuring the program, or it can even be something that could even be modified by malware.

I mean, there are a lot of different ways in which this kind of mistake can happen.