best and effective way to rename the file - c#

I have a wpf application and inside using Image resizer as a third party tool in that app. I am trying to achieve the following scenario:
I have text box (Employee Name) for example "Madona"
I have a image text box linked with third party tool "Image Resizer".
If i choose the image (eg: Madona123.jpg), the Image resizer will generate 17 different size images and drop it in my source folder (e.g. Madona123_80x60.jpg, Madona123_150x150.jpg, etc..)
But am trying to change the file name like e.g: Madona_80x60.jpg, Madona_150x150.jpg
I have two options but not sure which one is effective
Create Temp Dir from the source folder where the image is there and change the name based on the "Employee Name" and pass that image to "Image Resizer" tool
Once the Image is generated in the destination folder,then change the file name.
Am not sure which one is best and what other concerns i have to look ,thanks for your input.

Option 1 has an extra copy command - you can rename when you copy. But with option 2 you have multiple renames. I would do 1 for simplicity.

From your suggested scenarios I would go for option 2.
As you stated, creating an extra temp folder might cause rights issues so if you're not entirely certain about the setup of the machines that will be using the app then option 1 might be causing your app to fail.
As a secondary (and very minor) reason, option 2 will, even if it doesn't fail on rights still create an extra folder. If you would choose to make that temp folder part of your application, people have the tendency of removing temp folders and their contents.. Or you might create it every time on startup of the app, but that also requires, a little bit, coding. (altough point taken, this is just a minor reason and not really a reason that would tip the scales..)
I do agree with Blam's statement that option one would make the base coding a bit more simple but I think aldo the bit of extra work, option 2 will give you less possibility for failure.
So if you're gonna be using it in a manageable environment where you know the setup of your targetmachines, go with option one, it requires less and more straightforward coding, if you're gonna be using it in any other sort of environment, go with option 2. It 'll give you the highest chance of success.

Related

c# Storing resources(specifically images) in folder structure

If I want to add pictures, I have to put those into the resources and access them from the picturebox.Image property by using this:
AddPicturesFromOtherFolders.Properties.Resources.myPicture
Thats the only way I know. That works fine if I have 10 or so images, but what if I had 500 images? Nobody could keep track of anything. So I would like to structure these hyperthetical 500 Images in a folder structure which I could then access with something like:
pictureBox1.Image= ../../Assets/img/specialImages/myImage.png
That would be very neat, but I have found no way, that involves 100% C# code.
I would be wuite grateful, if you could help me.
Have a nice day,
Alexander Lenssen
You could use Image.FromFile and load the image from any file you have stored in your file system. For example:
pictureBox1.Image.FromFile(#"D:/Assets/img/specialImages/myImage.png");
There is no way that involves 100% C# code. At least some Compiler options or Setup actions are nessesary. But the first question is even where to store it: Programm Directory or UserProfiles?
Asuming these images are static (will only change when a installer runs), you can just store them into the Programm Directory. And from there deploy them with the rest of the code. Getting them Into the Output directory is not that difficulty. Visual Studio has options for that: https://msdn.microsoft.com/en-us/library/0c6xyb66.aspx You could go further, like having a Shared Repository for Images (i.e., most Photoshop programms have one Content Folder under Programms).
You can go as far as "soft linking" them, wich means you can have one actuall folder on your disk that will be copied/synched into the output directories on any buil.
If you need to Update those Images them on the fly (without adminsitrative rights), stuff becomes more complicated. You can still do it via the SpecialFolders. CommonApplicationData seems like the right place to put this kind of stuff. Even Steam and Minecraft's old Java Launcher do quite some storage there. Not to mention every WebBrowser.

Dynamically add Image to ImageList that lasts in C#

I have this code to add an Image to ImageList :
imageList1.Images.Add(Image.FromFile(path));
I want to somehow save the image in my application(resources maybe?!) just like the way you add Image to ImageList at design time so if I move my application files somewhere else the added images move with it. I don't want to save files in application path or database or other things like that.
Is there any way to do that ?
Can you logically explain how that should work? Did you think this through?
just like the way you add Image to ImageList at design time
If you do this, they get COMPILED INTO THE PROGRAM AS RESOURCE. Which not only requires the compiler and the source code (though you can put them into a resource only assembly), but also access to changing the program files.
Doable at compile time, totally not a sane approach at runtime.
It also effectively stored them in a resource assembly in the application path, which you rule out as a location.
I don't want to save files in application path or database or other things
like that.
latest "other things like that" would ALSO rule out modifying the program (as it would store them somewhere) and make this a total fallacy request. You want to store images but not store them. Grats. Even if not:
Programs should NOT NEVER EVER modify themselves. This is a high priviledge operation, normal users can not change the program files folder.
As you rule out all other places - where you want to store the iamge? Cloud? Magic?
There is no way to do what you want because you rule out all possibilities. And "Like at design time" only seems to think you think this works by magic.
So, no - the question as you have asked it has one answer: get realistic. You can not rule out all ways to save them and then want them saved. Requirements contradict themselves.

storing ntfs file metadata for retrieval via webserver and linking with database

I am looking for a few pointers to reaching am adequate solution to a problem/feature I need to implement/rectify in my asp.net mvc application.
My application is a LAN only interface that is run over a webserver. In this application there is a page that displays a bunch of files/folders.
I need to be able to store a set of attributes\properties about these files, and those props\attrs need to be independent of their location on the fileserver. This is my main issue, as I could easily link them to the db with the path as the primary key, but alas then as soon as the file moves their link to the db would be lost.
The types of files that need to be displayed unfortunately could be anything. .txt, .exe, media etc etc. So that provides a limiting option also from using something like the tagsharp lib.
One approach i was considering was simply storing a key somehow in the file itself, or with an ADS ( i have no experience in doing this, but am presently trying to research its potentiality).
Does anybody have any experience with this issue, and can recommend a simple approach. I am hoping i do not need to implement an ADS approach as what ive been reading so far is a little bit over my head and im not sure C# will handle the streams adequately for my needs.
Opinion based. Proposal anyway: what about an additional file which is found by a naming convention?
MyDocument.doxc
MyDomument.docx.properties
MyMovie.mp4
MyMovie.mp4.properties
When moving / renaming files, make sure you move / rename the properties file the same.
First of all thanks for taking a moment to reply.
I had considered the possibility of using a separate file. The problem is that the users of the filesystem ( which may or may not include users of the lan application ) need to be able to move/copy files independent of db application.
Therefore if a user moves a file in windows explorer, I need it to automatically move those additional properties with it. Unfortunately I cant rely on users to move those additional files on their own volition, and I cant ask users to only use the application to move files ( if i were to generate code for the program to do this ).

Calculating and caching folder sizes

I have an idea for a C# program that works basically like the Windows Explorer. The aim is to display all files and folders, and to show specific information for each of them. One of the features I'm planning is to detect folder sizes which is something the Explorer cannot.
My idea for the algorithm is to cumulate the sizes of all files in the specific folder. However, I'm afraid of performance issues. For example, for displaying the sizes of all folders of C: I have to consider all the files on the whole drive. This will probably take a while and thus the calculation can't be done each time the user switches to a different folder or back.
So I'd like to cache some of the sizes. However, when files change, are added or removed, the cache data becomes outdated. But I do not want to monitor all file changes while the program is not running.
Is there any way I can find out if the cache is up-to-date, e.g. by retrieving some sort of checksum that doesn't require calculating all sizes again? Is there another memory and CPU-efficient way to find out if file sizes have changed since the last calculation? Or is there even another possibility?
Windows Explorer has the Folder size available (# files, size on disk etc) availble for the properties of any disk/folder. Directory Properties Example
As for writing a program, you can certainly use a recurisve DirectoryInfo.EnumerateFiles() to get all the files within a disk/folder.
As for monitoring, you can use the FileSystemWatcher class to monitor changes to any disk/folder.
To keep the cache up to date is going to be difficult because:
Depending on the Partition Formated Type [Fat, Fat32, NTFS, etc] you are limited to what each support.
Any new file (created date > cache date) means you still have to enumerate all the files to filter the list to new files.
Modified files (modified date > cache date) has the same issue.
Unless you use something VERY specific to the Formatted Type beyond what C# provides, updating a cache after the application launch will need to occur every time, and be very intense.
Windows Explorer is a pretty crafty program. It is filled with tricks that are designed to hide the fact that any file system is punishingly slow to iterate. The kind of tricks that I know about:
fake it. Show the folder hierarchy as a treeview and use the [+]
glyph to show that a folder has files or directories inside of it.
Even when it doesn't. That's visible, create an empty directory and
restart your machine. Note the [+] glyph, click it and notice that,
when forced to iterate the sub-directory, it smoothly change the [+]
glyph to a 'nothing there' glyph.
delay it. Harder to see, you need a subdirectory with a lot of
files. Explorer starts a background thread that iterates the
content of the folder. Once it figured it out, it smoothly changes
the status bar text.
tell me what happened. Explorer uses ReadDirectoryChangesW()
heavily. Wrapped in .NET by the FileSystemWatcher class. Key point
is that it gets a notification that something changed in the
subdirectory that the user is looking at. No polling required, that
would have horrible perf. Go back to bullet two.

How to know the next temp file to be created in windows?

I am by no means a programmer but currently am wondering if an application creates a temp file that windows names. For example the file it creates is tmp001, is there a way i can take that name tmp001 and ask windows to give me the next temp file it would create before it creates it.
Thanks,
Mike
There is no direct means to get to know the next temporary filename to be created.
For example, programmers use the System.IO.Path.GetTempFileName method, but one can add application-specific prefixes or suffixes in order to make it easier for the application to find its newly created files.
One can even choose to save this temporary file elsewhere than the system Temp folder.
You would need to define a "temp file" much more explicitly in order to answer this question with a "Yes". The problem is that a "temp file" is just something not meant to be kept. It could exist anywhere on the system and be created by a user, application, or service. This would make it nearly (or actually) impossible to answer your question with a "Yes".
If you constrain the definition of a temp file to just the files in the official temp folder (or a subfolder), you still have a problem if you're trying to catch names not generated by windows. Any app could produce a particularly named temp file in that folder, without Windows caring.
If you further constrain the definition to be only those files named by Windows, you might be able to get somewhere. But, does that really meet your needs?
After all of that, maybe it would be better to describe the problem you're trying to solve. There may be a much better (workable) solution that would address the issue.
Typically applications use the Win32 API GetTempFileName to get the temporary directory.
The process of how the temp file is generated is described there.
I'm not sure why you want this info, but perhaps you could for example register for directory changes via a Win32 API like ReadDirectoryChangesW or by using a mini filter driver.
This kind of code just cannot work reliably on a multi-tasking operating system. Another thread in another process might pre-empt yours and claim the file name you are hoping to create.
This is otherwise easy enough to work around, just name your own files instead of relying on Windows doing it for you. Do so in the AppData folder so you'll minimize the risk of another process messing it up.

Categories