Open Resource File with FileStream fails [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
I want to open a Resource File with the FileStream Class. It's a text File and I want to read it Line by Line.
FileStream fs = new FileStream(Properties.Resources.Testing, FileMode.Open, FileAccess.Read);
The called Exception is System.ArgumentException and it says there is an invalid character.
I hope anyone can help me to fix this, or if theres a better way it's also ok but I need the File in the .exe so it needs to be a Resource..

When you add a text file as a resource, it will be embedded as a string. So your FileStream constructor call will assume that you are trying to open a file on disk with a name that is the same as the text file content. That ends poorly of course.
It isn't very clear if you really want a stream, the string tends to be good as-is, you might consider the String.Split() method to break it up into lines. Or maybe you like the StringReader class so you can use ReadLine():
using (var rdr = new StringReader(Properties.Resources.Testing)) {
string line;
while ((line = rdr.ReadLine()) != null) {
// Do something with line
//...
}
}

Related

C# How do I prevent a file from saving without the asked extension? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 1 year ago.
Improve this question
I'm currently writing a program and using my own file extension ".pn" for these files. When I use the BinaryWriter in FileMode.Create to write to the file "primes.pn" and then look in the file explorer it does not have the extension I asked. My file explorer has display file extensions turned on. How could I prevent this from happening/what is causing this?
Additionally the check for whether "primes.pn" exists passes but then when checking the length of the file it throws a System.IO.FileNotFoundException.
if (!File.Exists("res_divisibility/primes.pn") && bytes.Count > new FileInfo("res_divisibility/primes.pn").Length)
{
BinaryWriter bw = new BinaryWriter(new FileStream("res_divisibility/primes.pn", FileMode.Create));
foreach(byte b in bytes)
bw.Write(b);
}
Still not sure what was causing the problem but File.WriteAllBytes()solves this issue. The existence check was my mistake because I was using && instead of ||.

(ObjectDisposedException) cannot upload FormFile to disk space [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 2 years ago.
Improve this question
I need to unit-test method
Here I instantiate new FormFile from disk
Here I use static method,with file and path to folder
var id= await ContentSaver.Save(file, path + "\\");
Than I try to save that file back to disk
And VS throw an exception "System.ObjectDisposedException : Cannot access a closed file."
How to solve it?
This is stacktrace
The answer at your first code part (https://i.stack.imgur.com/ktjip.png). You create the stream in using scope and dispose it at once. To avoid it copy the content of your file into MemoryStream (do not need to be deposed) and set as source of FormFile
IFormFile formFile;
using (var fstream = new FileStream("path", FileMode.Open))
{
var mstream = new MemoryStream();
fstream.CopyTo(mstream);
formFile = new FormFile(mstream, 0, mstream.Length, null, mstream.Name);
}
// here fstream is disposed, but not mstream, and you can use your FormFile instance
// MemoryStream does not need to be disposed explicitly, it do not posess any OS specific handlers, GC is enought.

Why does not read the whole text file? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
In C#, I would like to read a .tga picture into a string variable.
I use many of variations to read from a text file, but there is a problem with every solution.
The file size 17Kb
Why doesn't read the whole text?
For example this does not work:
string item = "";
while ((item = sr.ReadLine()) != null)
{
picture_string += sr.ReadLine()+"";
}
It does not work:
picture_string = sr.ReadToEnd();
It does not work
picture_string = File.ReadAllText(path);
The file you are trying to read is a binary file, not a text file. Stop trying to read a binary file as if it is a text file.
var fileContents = File.ReadAllBytes(path);

File Not Found when opening to read a file [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 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
}

C# - Extract one *.jar file into another one [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to do my own Minecraft Launcher for Moded Minecraft. And I have a issue: I need to extract content of one *.jar file to another. I tried a lot of things and I am totally desperate.
Basicly, I need to do this in code.
As Andrew briefly mentioned, there are some good .NET libraries for manipulating zip files. I've found DotNetZip to be very useful, and there are lots of helpful worked examples here.
In answer to your question, if you just want to copy the contents of one *.jar file to another, keeping original content in the target file, please try the following. I'm using the .NET zip mentioned above (which also has a NuGet package!):
using (ZipFile sourceZipFile = ZipFile.Read("zip1.jar"))
using (ZipFile targetZipFile = ZipFile.Read("zip2.jar"))
{
foreach (var zipItem in sourceZipFile)
{
if (!targetZipFile.ContainsEntry(zipItem.FileName))
{
using (Stream stream = new MemoryStream())
{
// Write the contents of this zip item to a stream
zipItem.Extract(stream);
stream.Position = 0;
// Now use the contents of this stream to write to the target file
targetZipFile.AddEntry(zipItem.FileName, stream);
// Save the target file. We need to do this each time before we close
// the stream
targetZipFile.Save();
}
}
}
}
Hope this helps!

Categories