Use a ShellFile object using Windows API Code Pack for Microsoft - c#

Windows API Code Pack for Microsoft can be downloaded from here. That is a really nice library and it has great examples. For example if I open the solution WindowsAPICodePack10 that comes in the zip from downloading the code pack (it only contains the libraries I added a win forms and wpf application)
then I am able to use the library very easily for example in my wpf application I can drag:
ExplorerBrowser user control (note I have to add references to the libraries that came with the solution)
and then with a button I can populate that control with this lines of code:
// Create a new CommonOpenFileDialog to allow users to select a folder/library
CommonOpenFileDialog cfd = new CommonOpenFileDialog();
// Set options to allow libraries and non filesystem items to be selected
cfd.IsFolderPicker = true;
cfd.AllowNonFileSystemItems = true;
// Show the dialog
CommonFileDialogResult result = cfd.ShowDialog();
// if the user didn't cancel
if (result == CommonFileDialogResult.Ok)
{
// Update the location on the ExplorerBrowser
ShellObject resultItem = cfd.FileAsShellObject;
explorerBrowser1.NavigationTarget = resultItem;
//explorerBrowser1.Navigate(resultItem);
}
and after that I am able to have something like:
That is amazing but I don't understand Microsoft. If they give you those libraries they should make it easy to customize that user control. the reason why I downloaded those libraries is because I need to place files from a specific directory on a stackpanel and be able to have the same functionality that you get with files on explorer (able to drag files, get context menu when right clicking file, dropping files to that container etc)
anyways I don't need all that functionality. from studing the library I think that user control contains a ShellContainer object and it's childern are ShellFiles maybe.
So from this library I will like to create a ShellFile object and place it in a StackPanel. after tedious studing of the library I finally found out how to instantiate an object from shellFile (ShellFile class is abstract) :
string filename = #"C:\Program Files (x86)\FileZilla FTP Client\filezilla.exe"; \\random file
ShellFile shellFile = ShellFile.FromFilePath(filename);
now it will be nice if I could place that file in a container. I am not able to instantiate a ShellConteiner object becaue it is abstract too. so how Will I bee able to place that shell file on a canvas for example?
or maybe I could extract the properties that I need and create a user control that will represent a shellFile. I know how to get the thumbnail I can do something like:
string filename = #"C:\Program Files (x86)\FileZilla FTP Client\filezilla.exe";
ShellFile shellFile = ShellFile.FromFilePath(filename);
System.Drawing.Bitmap btm = shellFile.Thumbnail.ExtraLargeBitmap;

Related

UIDocumentInteractionController always creates a copy

I'm currently developing a Xamarin.iOS App that gets a document from a web service that should then be edited offline on the tablet after being downloaded to the internal storage.
The most common answer was to use the UIDocumentInteractionController. However if I use the UIDocumentInteractionController then I can only create a copy of my original file and open this copy. To get it back into my app I have to make the user select the document from the 'UIDocumentPickerViewController'.
Is there a better way to make the `UIDocumentInteractionController' not create and open a copy of the original, or to at least get the url from the new documentcopy?
Code to open the file:
public void OpenFile()
{
var url = NSUrl.FromFilename(FilePath);
var controller = new UIDocumentInteractionController();
controller.Url = url;
controller.PresentOpenInMenu(table.Frame, table, true);
}
If that is not Possible: Are there different tools or controlls i could use to open and edit a MS-Office file directly?

How to create resx files

I am trying to use Icons in a windows form application. I read that you can use resx files to do this. (I also read that resx files can be used for localization but this is not the point of this question)
I know more or less how to use a resx file if I have one(see below). What I don't know and I can't find anywhere is how to create these resx files (I know what these files are already)
Can someone teach me how to create a resx file that holds information on icons non-programmatically (the few responses about this in SO are programmatical ones)
The objective is to have a "resource.resx" file in my project that holds data about a "myicon.ico" file.
I am planning to use this as in
ResourceManager rm = new ResourceManager("resource",
typeof(Resource).Assembly);
Object ret= rm.GetObject("something");
if(ret!=null && ret is Icon)
return (Icon)ret;
else
return null;
Please don't point me to this link since I have already read it and could not find a practical way to do what I asked.
Assuming you're using Visual Studio:
Right-click your project
Select Add | New Item
Select Resources File
Give it a name (e.g. Resources)
Click Add
You will now have a Resources file in your project with the name you provided, and it should auto-open the resources editor. If it doesn't, double-click it in the project.
In the top-left of the editor, click the "Strings v" drop down and select "Icons".
Drag your icon into this screen.
Rename it to whatever you want (e.g. something).
If you named your resources file "Resources", you should be able to access the icon like so:
Object ret = Resources.ResourceManager.GetObject("something");
if (ret != null && ret is Icon)
return (Icon)ret;
else
return null;

Embed File in a Executable

I'm trying to build a simple program and I want to find a way to embed a file (or multiple files) in the executable.
The program is very simple. I will be building a form using C# in visual studio. On the form, there will be couple questions and a submit button.
Once the user has answer all the questions and hit the submit button, if all answers are correct, I want to give the user the file as a prize. (The file can be image, video, or a zip file that contains multiple other files)
The way I want to give the user the file is very flexible. It can just be creating this file in the same directory as the executable, or given the download option for the user to save it somewhere else.
Below is the pseudo code
private void submit_Click(object sender, EventArgs e)
{
//functions to check all answers
if(all answers are correct)
{
label.Text = "Congrats! You answered all questions correctly";
//create the file that was embeded into the same directory as the executable
//let's call the file 'prize.img'
Process.Start("prize.img");
}
else
label.Text = "Some answers were not correct";
}
The logic is pretty simple and straight forward. The problem is, how can I embed "prize.img" into the executable? I will be giving this program (.exe) to a friend so he will not have any source and I can't guarantee the path.
You want to do embed the file as a resource.
Right-click the project file, select Properties.
In the window that opens, go to the Resources tab, and if it has just a blue link in the middle of the tab-page, click it, to create a new resource.
In your code you can type in Resources.TheNameYouGaveTheFileHere and you can access its contents. Note that the first time you use the Resources class in a class, you need to add a using directive (hit Ctrl+. after typing Resources to get the menu to get VS to do it for you).
Do you need help with the saving of the file also?
Edit:
You could do something like this:
var resource = Properties.Resources.yourResource;
FileStream fileStream = new FileStream("filename.exe", FileMode.CreateNew);
for (int i = 0; i < resource.Length; i++)
fileStream.WriteByte((byte)resource[i]);
fileStream.Close();
Does it help you?
EDIT:
As I can see you are getting a stream, here is an update to make it work:
var resource = Properties.Resources.yourResource;
FileStream fileStream = new FileStream("filename.exe", FileMode.CreateNew);
resource.CopyTo(fileStream);
fileStream.Close();
Does it work now?
Can't you add the image file as a resource in your solution? And then you can reference that resource in your code?
See if this may be what you're looking for:
Embedding Image Resource in Project
The link provided offers step-by-step how to accomplish this:
After adding the image as a resource, the instructions show how to access your image resource and use it in a program. Just click the link above and follow the instructions.
I followed the instructions above and wrote my program like this:
using System.Drawing;
using System.Windows.Forms;
using System.Reflection;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var myAssembly = Assembly.GetExecutingAssembly();
var myStream = myAssembly.GetManifestResourceStream("WindowsFormsApplication6.Desert.jpg");
var image = new Bitmap(myStream);
pictureBox1.Image = image;
}
}
}
My output looks like this:
The image on the form came from the image that I embedded as a resource. Very easy to do.
Add the file into project then set its Build Action as Embeded resource.
From your code you can do something like this:
var _assembly = Assembly.GetExecutingAssembly();
var _stream = _assembly.GetManifestResourceStream("namespace.fileneme");

Simply get files from internal project folder [duplicate]

This question already exists:
Closed 10 years ago.
Possible Duplicate:
how to populate a listbox with files in the project of my windows phone 7 application
I'm a newbie on C# and this is annoying me a lot.
My application load a set of images from a folder that I simply created on the Solution Explorer called Images. I can see these images if I use it hardcoded with URIs and stuff, but what I want to do is to take these images names dinamycally and then load it. I have seen some questions like it but couldnt solve my problem. I'm trying like this:
DirectoryInfo directoryInfo = new DirectoryInfo(#"/Images");
foreach (FileInfo file in directoryInfo.GetFiles()) {
photos.Add(new Photo() { FileName = file.FullName, PhotoSource = GetImageSource("Images/" + file.FullName) });
}
The directoryInfo is always set as null. My project hierarchy as shown in Solution Explorer is like:
Project
Main.xaml
Maim.xaml.cs
Images
1.jpg
2.jpg
...
Thanks in any help.
From MSDN:
For a Windows Phone application, all I/O operations are restricted to
isolated storage and do not have direct access to the underlying
operating system file system or to the isolated storage of other
applications.
So you can't access your Images folder in the manner you'd like.
Since you can't add images dynamically to your XAP anyway, the images available will be constant. It appears you will just have to add the URIs manually:
BitmapImage myImage = new BitmapImage(new Uri("Images/myImage.png", UriKind.Relative));
If you've got different images for different locales, you could include the image name/path in a resources file and then create them from there.
Alternatively if you have a set of default images and will then have some user-added ones, and you'd like to iterate over all of those, you could write your defaults from your Images folder into IsolatedStorage at first start-up. See here for details on using IsolatedStorage. You can iterate over directories and files within the apps IsolatedStorageFile (see the methods available).
Maybe you want to use
string path = Path.Combine(Environment.CurrentDirectory, "Images");
foreach (string file in Directory.GetFiles(path)
{
photos.Add(new Photo {FileName = file, PhotoSource = GetImageSource(Path.Combine(path, Path.GetFileNameWithoutExtension(file))});
}
which returns a List of the Contents of the Folder.
Path.Combine combines single strings to a full Path (So you don't need to worry about the Backslahes.
The Path Namespace is pretty underrated, i'd suggest you to take a close look to it since it will save you much time.

Customising the browse for folder dialog to show the path

What is the simplest way to customise the System.Windows.Forms.FolderBrowserDialog so a path can be entered using text in a textbox below the tree? This would make it easier to select unmapped UNC paths.
Looks like this KB has some supporting information.
Just this weekend I needed this. I looked and looked but could not find it. Resorted to writing it myself, based on that KB article, and some other things. Here ya go. FolderBrowserDialogEx (article in archive)
Full Source code. Free. MS-Public license.
Code to use it:
var dlg1 = new Ionic.Utils.FolderBrowserDialogEx();
dlg1.Description = "Select a folder to extract to:";
dlg1.ShowNewFolderButton = true;
dlg1.ShowEditBox = true;
//dlg1.NewStyle = false;
dlg1.SelectedPath = txtExtractDirectory.Text;
dlg1.ShowFullPathInEditBox = true;
dlg1.RootFolder = System.Environment.SpecialFolder.MyComputer;
// Show the FolderBrowserDialog.
DialogResult result = dlg1.ShowDialog();
if (result == DialogResult.OK)
{
txtExtractDirectory.Text = dlg1.SelectedPath;
}
Capabilities: shows editbox, shows full path in edit box. Can be used to browse printers or computers, as well as files+folders, or just folders.
Edit, 2018-05-31:
If the Codeplex link above does not work for you, this Git resource also exists.
Edit, 2022-02-11:
There is probably new repo of original author https://github.com/DinoChiesa/DotNetZip/blob/master/Zip/Resources/FolderBrowserDialogEx.cs
Try under code project folder browser - this allows customizing the dialog in many ways.
Also in social.msdn.microsoft.com there is a post that suggest creating a form of your own for that and even suggest the code for it.

Categories