pretend to have a file without loading it from harddrive [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How to pretend to have a file without actually loading a file? The challenge is not to create a temporary file or load anything from a harddrive. I would rather like to keep everything in "memory".
In memory means to me having an existing class which derivates from stream initalized and having that class working like it loaded a file from harddrive but it didnt...

A memory stream isn't a file. It's just data. The concept of setting a filename and extension on a memory stream simply doesn't make sense - any more than it would for a byte[].

You can create a subclass to keep track of the filename, like so
class MemoryStreamWithFile: MemoryStream
{
public string Filename { get; set; }
}

Related

Returning a list of objects from a function to a text file C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a big function that returns a list of objects. I basically need the results to be put in to a text file. The list contains objects of a class with attributes string Index and integer count. I would want it to be written in a textfile like:
Index : Count fe.
BOOK_FROM_STORE : 27
Anybody has some guidance?
Parse the data held by your object to string and then write it to a text file.

Any reason to save a file before converting to Base64 to pass on? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I'm creating an attachment section for a webpage that takes said attachment, converts it to base64 and passes it to xml gateway.
Is there any reason for me to save said file locally? The file size limit is 5mb.
Current set up seems to work;
byte[] attachBytes = new byte[attach.ContentLength];
using (BinaryReader theReader = new BinaryReader(attach.InputStream))
{
attachBytes = theReader.ReadBytes(attach.ContentLength);
}
model.filename = attach.FileName;
model.base64convert = Convert.ToBase64String(attachBytes);
VS Saving(File isn't required to be stored on the server)
attach.SaveAs("C:/temp/Attachfiles/" + attach.FileName);
byte[] attachBytes = new byte[attach.ContentLength];
using (BinaryReader theReader = new BinaryReader(attach.InputStream))
{
attachBytes = theReader.ReadBytes(attach.ContentLength);
}
model.filename = attach.FileName;
model.base64convert = Convert.ToBase64String(attachBytes);
Mainly just wondering the downsides of doing it how I am. I'm thinking there is no difference as I am not reading the saved file back in but I'm still learning and don't want to miss anything.
Your question is a bit open ended, but answerable.
If you have to have some record of receiving the file before sending it on, then save it. However, there are downsides to saving a file
you could run out of space
the file, if it contains anything sensitive, is stored in the clear
you're introducing a point of failure (e.g. can't save file) for no gain
So, as specified, don't do it. If your requirements evolve you can always re-evaluate that decision.
As mentioned in the comments, a save may be reasonable if you are doing something like a resend in case of failure; but even then I suggest not keeping the file around forever, and usually storage at/on a web server is a bad idea for the reasons above.

Is .tif image multipage or not? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a simple question (I hope it is simple for someone). How could I recognize is .tif (or .tiff) image contains two or more pages, or it is just one image (one "page"). I use .NET. For what? I have an image as input and should process it in some way, and if image consists of several images - choose one method, just one image - another method. I am waiting for any free solution (it can be just .net or any free third-party library). I don't need to split tiff or any other good thing, just something like
Boolean isMultipage = SomeLibrary.IsTifMultipage(filePath);
Thanks!
You could write a method that would determine that for..Perhaps something like this?
public bool IsMultipage(string fileName)
{
using (Image imageFile = Image.FromFile(fileName))
{
FrameDimension frameDimensions = new FrameDimension(imageFile.FrameDimensionsList[0]);
return imageFile.GetFrameCount(frameDimensions) > 1;
}
}

Detecting if the file is CSV without checking extension in C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How can I check if a file that is in a directory is a CSV file without checking its extension?
If the file extension is not a mandatory for your program design, but only format of the file (csv in your case), the only valid way to check if a given file is either "ok" for you or not, is simply to check your data structure after you populated it from the file.
The basic flow in gross may look like this:
1) Select file
2) Read the file
a) Exception happens somewhere = non valid CSV formatted file
b) All is ok
3) Validate datastructure(s) populated from the file
a) Some mandatory fields are not initialized = non valid CSV file
b) All is ok

Get Resources with string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a lot of txt files in Resources folder. One of them is corner.txt. I can access this file via this code snippet:
Properties.Resources.corner
I keep file names in string variables. For example:
string fileName = "corner.txt";
I want to access this file via:
Properties.Resources.fileName
Is this possible? How can I access?
I solved the problem this code snippet:
string st = Properties.Resources.ResourceManager.GetString(tableName);
So, I don't use the filename, I use the txt file's string. This is useful for me.
Thanks a lot.
You can use Reflection like that:
var type = typeof(Properties.Resources);
var property = type.GetProperty(fileName, BindingFlags.Static| BindingFlags.NonPublic|BindingFlags.Public);
var value = property.GetValue(null, null);
or use the ResourceManager like that:
value = Properties.Resources.ResourceManager.GetObject(fileName, Properties.Resources.Culture);

Categories