How create custom file attribute via c# - c#

Good day!
I have some file called "*.dat" with text into it.
So, i try to create attribure "Version" ,but don't know how.
Can i do it via c#? Can you write some examples?
Such like this?
File.SetAttributes(path, attributes);
Thank you!

You can't. The only values allowed are from the list here

You're probably struggling because you can't add arbitrary information into a file. There are a known set of attributes you can change using the FileAttribute properties
What you would normally do is provide some information at the start of your file, typically called the file header. This then allows a custom reading implementation to read out the version, without having to read the rest of the file. This is quite standard practise with all the files you're used to, for example a WAV audio file:

The attributes you can change here are the NTFS attributes. Details here:
http://msdn.microsoft.com/en-us/library/system.io.fileattributes%28v=vs.110%29.aspx
Version is a resource embedded in an executable file. A similar question was asked here:
How do I set the version information for an existing .exe, .dll?

reating "custom" file attributes is not allowed.
You can only set one of the FileAttributes .
Example:
File.SetAttributes(#"C:\myfile.txt", FileAttributes.Hidden);

Related

Manage file custom properties (Get&Set) programmatically - C#

I'm implementing a C# Windows console application to manage files in Windows Explorer. At this point of my work I need to create file custom properties and both set and get their values. I've read many web articles, and I understand that I can't do this for each file, it depends on the type/nature of the selected file... and this is ok for me, this is a limit that I took into account and accepted before starting my work.
Please consider that I want to manage these properties without using the file related application (for example, in case of a .docx file I don't want to open the Word application and then work with Microsoft.Office.Interop.Word.Application and Microsoft.Office.Interop.Word.Document classes). Cases do in fact exist where it is possible to right-click on a file in Win Explorer, select 'Properties' and then find a tab named as 'Custom' where you can search, read and set custom properties.
What I want to do is manage (Read & Write) this file Custom information programmatically.
Any hint? Thanks!
EDIT #1:
I tried to follow the Simon Mourier's hint but unfortunately it does not work, I was not able to SET the property value. I tried also the Rod Howarth's hint but it fails when you try to set the value of an existing custom property... moreover, using the DSOFile library there are problems related to the persistence of the saved custom property.
See this. It is about office file custom properties, but there is one answer describing the method how to get custom properties of any file (as far as I anderstood, even txt file can have some)
This solution seems working fine if you need to set the value of an existing custom property; if you need to create a new born custom property, then you must use the Add(string sPropName, ref object Value) method of the CustomProperties collection.

Get Shell Icon with only a file extension

I want to get the windows system icon(s) for a file type without necessarily providing an existing file path (required by SHGetFileInfo)
Is this possible, or do I have to resort to creating temporary files or reading the registry?
Yes is possible using file extension as first parameter in SHGetFileInfo.
You must also use SHGFI_USEFILEATTRIBUTES flags.
In case you were interested in an implementation I had a blog post on this in 2004 that can still be found here (about the third entry down) Old blog post

Add Pdf file to Resource using Code C#

Is there any way to add a file to an application's resources using code (not the visual designer)? I've tried the ResourceWriter class but it doesn't do what I want.
The ResourceManager class is only used for getting resources. If you want to add or edit resources, see the MSDN page on Adding and Editing Resources, or look at the Resource File Generator for dynamically creating resource files.
Edit: The ResourceWriter class will probably do what you want, too... did you read the remarks on how to use it? Generating resource files is simple, you would just have to programatically insert them into your project if that's where you wanted them ultimately.

Is it never possible to get the FullName from a file using Silverlight OpenFileDialog?

I want to get the fullname from a file on Silverlight OpenFileDialog, when I try that, Silverlight throws me an error.
I saw there is an attribute on FullName saying it is [SECURITY CRITICAL], but I need to display the full path, is it really no way I can do that?
OpenFileDialog won't provide the full name simply because it doesn't want you to alter those files. With Silverlight, you only have access to the isolated storage and the file could be located outside this storage.
But you could just open the file and copy it to the isolated storage, and modify it there.Just discovered that bassfriend found this link too and posted it above. My mistake. Then again, the link is in the top-20 of Google. :-)
Another reason why you won't get the full filename is because that filename could contain sensitive information. For example, a file in the "My Documents" folder could expose the user login name.
Basically, it's a security restriction. You're not supposed to bypass it, even if it would be possible. If you would find a way around this, MS would probably release a security update to close that leak again...
Well, yes, Silverlight will not allow you to retrieve the full path information. Your topic seems to be closely related to this question. Maybe the answers there will shed more light on your question.
Try to use the File property as documented here:
Example,
MSDN
The Silverlight OpenFileDialog behaves differently to the standard forms OFD for security reasons. If you retrieve the SelectedFile, it actually returns a FileDialogFileInfo object which contains the Name of the file, rather than a path to the file. When you think about it, this makes perfect sense - you don't want somebody writing a piece of malicious code that can get a handle into your filesystem.

Copy Folder/File wiithout modifying attributes?

Is it possible to copy a file or a folder from one location to another without modifying its attribute data? For example if I have a folder on a network drive and it was created on 2/3/2007 and I want to copy it to my c: drive .. but leave the date/time stamp as 2/3/2007...is that possible?
I'm not sure if it is possible; however you can use the methods within System.IO.File and System.IO.Directory to reset these attributes back to what they were originally.
Specifically the SetCreationTime and SetModificationTime methods will be of most value to you in this case.
I did something as shown below:
File.SetCreationTime(tgtFile, File.GetCreationTime(srcFile));
File.SetLastAccessTime(tgtFile, File.GetLastAccessTime(srcFile));
File.SetLastWriteTime(tgtFile, File.GetLastWriteTime(srcFile));
When you copy a file, it will retain the modified date, however the created date will be changed. I doubt there will be an easy way to retain the created date.
https://learn.microsoft.com/en-us/dotnet/api/system.io.file.copy?view=net-7.0
The attributes of the original file are retained in the copied file.

Categories