Having trouble with this code in C#. Here is the error I get: An unhandled excep
ID: 3753026 • Letter: H
Question
Having trouble with this code in C#.
Here is the error I get:
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
Additional information: Could not find file 'quote.txt'.
I am pretty the file is where it should be: C:UserskeeneDocumentsVisual Studio 2015ProjectsProjectsFileComparison.csFileComparison.csinDebugquote.txt & quote.docx
Here's my code:
using System.IO;
using System;
class FileComparison
{
static void Main()
{
// creating a fileinfo object for text file
FileInfo textInfo = new FileInfo("quote.txt");
// creating a fileinfo object for word file
FileInfo wordInfo = new FileInfo("quote.docx");
// getting size of text file
double textSize = textInfo.Length;
// getting size of word file
double wordSize = wordInfo.Length;
// print the size of text file
Console.WriteLine("Size of text file is " + textSize);
// print the size of word file
Console.WriteLine("Size of word file is " + wordSize);
// print the ratio of file sizes to each other
Console.WriteLine("Ratio of text file size to word file size " + textSize / wordSize);
Console.WriteLine("Ratio of word file size to text file size " + wordSize / textSize);
}
}
Here is the update code with path info
using System.IO;
using System;
using System.Security.Permissions;
class FileComparison
{
static void Main()
{
// creating a fileinfo object for text file
FileInfo textInfo = new FileInfo("@.C:\Users\akeene\Documents\Visual_Studio_2015\Projects\Projects\FileComparison.cs\FileComparison.cs\bin\Debug\quote.txt");
// creating a fileinfo object for word file
FileInfo wordInfo = new FileInfo("@.C:\Users\akeene\Documents\Visual_Studio_ 2015\Projects\Projects\FileComparison.cs\FileComparison.cs.\bin\Debug\quote.docx");
// getting size of text file
double textSize = textInfo.Length;
// getting size of word file
double wordSize = wordInfo.Length;
// print the size of text file
Console.WriteLine("Size of text file is " + textSize);
// print the size of word file
Console.WriteLine("Size of word file is " + wordSize);
// print the ratio of file sizes to each other
Console.WriteLine("Ratio of text file size to word file size " + textSize / wordSize);
Console.WriteLine("Ratio of word file size to text file size " + wordSize / textSize);
}
}
Get this error:
An unhandled exception of type 'System.NotSupportedException' occurred in mscorlib.dll
Additional information: The given path's format is not supported.
Explanation / Answer
FileInfo textInfo = new FileInfo("@.C:\Users\akeene\Documents\Visual_Studio_2015\Projects\Projects\FileComparison.cs\FileComparison.cs\bin\Debug\quote.txt");
Error says the specified path format is not supported in fileinfo function call. The @ symbol should be placed outside the double quotes (string).
FileInfo textInfo = new FileInfo(@"C:\Users\akeene\Documents\Visual_Studio_2015\Projects\Projects\FileComparison.cs\FileComparison.cs\bin\Debug\quote.txt");
OR
string path = @"c:\check.txt";
FIleInfo info = new FileInfo(path);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.