File Not Found when opening to read a file [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I enter a path name and a file name into my program, that works enough, and eventually the path might be something like
path = #"C:\ ...\";
Where the ... is just the rest of the path to the directory where the below file is located.
file = "something.txt";
But I get a file not found when I execute this:
System.IO.StreamReader reader = new System.IO.StreamReader(path+file);
I'm confused as to what I'm doing wrong. Am I just not using this correctly?
I'm an idiot sorry, I found out what I was doing wrong.

You can try to use Path.Combine, but if you assign your full path directly to a single variable it would better,I don't understand why are you doing this by the way.
StreamReader reader = new StreamReader(Path.Combine(path,file));
Note: Delete last backslash from the path variable

Create a FileInfo object to make your life a bit easier.
FileInfo file = FileInfo(Path.Combine(path,file));
if(!file.Exists)
throw new FileNotFoundException("File Not Found or Inaccessable"); //or handle approprately
using(StreamReader reader = file.OpenText())
{
//do reading stuff here
}

Related

How to delete a file with no extension? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm developing a programm and the programm schould delete a cache. The only problem is that all the files in the cache have no extensions and always when I try to delete those files I get an exception: The access to the path 'path' was denied. So I don't know why this error occurs because I'm trying to delete a file and not a directory. I think it doesn't recognises the file as a file because of the missing extension.I've etried this:
DirectoryInfo cache = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + #"\Cache");
Console.WriteLine("Deleting Cache...");
foreach (var fi in cache.GetFiles())
{
File.Delete(cache + #"\" + fi.Name);
Console.WriteLine(cache + #"\" + fi.Name);
}
Console.ReadKey();
What else can I try?
It seems your file was still open, so you can't delete it. However, my original answer still applies:
Since you already have a DirectoryInfo, you dont need to concatenate the strings to get the file-name, just use FileInfo.Delete:
foreach (var fi in cache.GetFiles())
{
fi.Delete();
}
You should avoid concatenating strings to build a path anyway, use Path.Combine:
DirectoryInfo cache = new DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Cache"));

No overload for method 'ReadLine' arguments when only 1 argument is given [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am trying to read the first line in a file, but I keep getting this error every time I do sr.ReadLine(1); and I can not find an answer to this error.
My code:
using (StreamReader sr = new StreamReader(DataStore))
{
string Conent;
Conent = sr.ReadLine(1);
};
As per the docs, the method should be called with no parameters i.e.
sr.ReadLine()
Since I just had and solve a problem like you're facing. Just code
sr.Readline()
Also you might wanna add some Error minimization if you're going to read it line by line in a while/loop like this.
while ((content = sr.Readline()) !=n null && (line != ""))
{
your code
}

File.Exists returning True when should be False [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm working on a program which requires the lookup of files in user defined directories. If the files don't exist, it must continue to use it's own resources. The problem is, even though some files don't exist, the function File.Exists returns true for them.
Here is an image of the error:
Here is the code that looks for the file and produces the error:
string[] vmtLines = null;
if (File.Exists(vmtFile))
{
try { vmtLines = File.ReadAllLines(vmtFile); }
catch (Exception e) { Debug.Log(vmtFile + " Exists: " + File.Exists(vmtFile) + ", " + e.Message); }
}
It turns out the problem is not in C#, Unity, or my programming. It seems there's a problem with files that are called con. I was testing out in the cmd to see if I could open the file con.vmt with notepad, here's what happened:
Then I tried to see if the same thing happens with other files in the same directory:
It opened just fine. Then I tried to see if it were just 3 letter file names which was the problem by opening a file with the name com:
It didn't have some weird problem, then I tried to see if it was something with the extension vmt and the file name con:
It had the same problem. I tried the same thing in the parent directory, and the same problem occurred with the file name con. So I assume this happens anywhere.
Thank you #Programmer, #CharlesMager, #AlexK, #Quantic, #ScottChamberlain, and #LarsTech for trying to help. This was one strange problem

bitmap.save subfolder with string value [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
My code
string userid;
string userimageid;
bitmap.save(#"C:\temper\temp\"+userid+"\"+userimageid+".jpg");
get build error! why canĀ“t I create subfolder with string value name and insert image? still cant insert to that crated folder as name userid.
Yes, you can. But you must create first the folder and then save the bitmap:
string folderName = #"C:\temper\temp\"+userid;
if (!Directory.Exists(folderName) {
Directory.CreateDirectory(folderName);
}
bitmap.save(folderName + "\\userimage.jpg"); //your original concatenation had an error
Solved!
string folderName = #"C:\temper\temp\"+userid;
if (!Directory.Exists(folderName) {
Directory.CreateDirectory(folderName);
}
bitmap.save(folderName + "\\" +userimage+ ".jpg"); `Create first folder and save Thanks Mnieto`

C# Directory.GetFiles adds extra file extension to returned path [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
At the moment I'm currently trying to get all files from a specified directory. The files I'm looking for end with the file type .ship, however, my code returns "path\NameOfFiles.ship.ship" and I can't for the life of me decide where the extra file extension is added.
Here's the function I use
public static List<SaveEntry> GetAllSavesFromProfile(string ProfileName)
{
if (IsProfileMissing(ProfileName))
{
Debug.LogError("Couldn't find save from profile. Check if it exists before calling this function");
return new List<SaveEntry>();
}
List<SaveEntry> ListToReturn = new List<SaveEntry>();
List<string> SaveEntries = Directory.GetFiles(Application.persistentDataPath + "/Profile_" + ProfileName, "*", SearchOption.TopDirectoryOnly).Where(s => s.EndsWith(".ship", StringComparison.CurrentCultureIgnoreCase)).ToList<string>();
foreach (string entry in SaveEntries)
{
ListToReturn.Add(LoadShip(ProfileName, entry));
}
return ListToReturn;
}
I'm pretty I'm doing something wrong with the Where function, but I can't decide exactly where.
I suspect your filenames contains an extra .ship. You can verify that if you change your settings to show file extensions.
Also, you can use a search pattern instead of using Where
Directory.GetFiles(Application.persistentDataPath + "/Profile_" + ProfileName, "*.ship", SearchOption.TopDirectoryOnly)

Categories