private void Form1_MouseDown(object sender, MouseEventArgs e)
{
string[] files = new string[] { #"C:\directory\of\file\to\copy.txt" };
this.DoDragDrop(new DataObject(DataFormats.FileDrop,files), DragDropEffects.Copy);
}
This is the code that i used.
Well, it works well, but want to get the directory of the copied file.
How can I do this?
Using that the static Path class of System.IO,you can extract the path of directory
Path.GetDirectoryName(#"C:\Users\JNK\Desktop\2136D.png");
All you got is the return value of DoDragDrop() to see if the drop actually happened. What the receiving app did with the file is something you cannot find out. Could be anything, including not copying the file at all. A random example is only opening the file in a text editor, the behavior of VS and Notepad.
Note how the example you gave only drags from the desktop, not to the desktop. Use FileBrowserDialog if you need to know where the user wants to copy a file.
Related
I wanted to start a .exe file in a different folder but I wanted other people to also use it and I've been trying many things but it just keeps opening the file that the program that I'm creating is in. (I'm new to c#).
My ex of ^: \Desktop\VSCheatDetector\CheatDetector.exe(the program) and another regular file named viper_screenshare_tool and it has CheatDetector.exe (which I want to open when I click a certain button)
Code:
private void cheat_smasher_click(object sender, EventArgs e)
{
string dir = AppDomain.CurrentDomain.BaseDirectory;
Process.Start(dir, "vipers_screenshare_tool\\CheatDetector.exe");
}
You don't want to use AppDomain.CurrentDomain.BaseDirectory; - I'd suggest using App.config or something like that instead.
I have posted - How to use OpenFileDialog to select a folder?, I couldn't find the correct answer.
So, I have changed my question.
I want to customize OpenFileDialog to select multiple folders and files. I tried to find a solution and could see some posts about it.
From the internet, I found the following project - https://github.com/scottwis/OpenFileOrFolderDialog.
However, while using this, I faced one problem. It uses the GetOpenFileName function and OPENFILENAME structure from MFC.
And OPENFILENAME has the member named "templateID".
It's the identifier for dialog template. And the sample project has the "res1.rc" file and, also have the templated dialog in it.
But I don't know How can I attach this file to my C# project?
Or is there any other perfect solution about - "How to customize OpenFileDialog to select multiple folders and files?"?
If you use the FileNames property instead of the FileName property, you get a string array of each file selected, you select multiple files using the shift key. Like so:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog x = new OpenFileDialog();
x.Multiselect = true;
x.ShowDialog();
string[] result = x.FileNames;
foreach (string y in result)
MessageBox.Show(y, "Selected Item", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
For files and folders you need to use the CommonOpenFileDialog included with the WinAPI, the particular class is here.
Try this:
openFileDialog.Multiselect = true;
You might not get a built in .Net control like that. Definitely the OpenFileDialog can not function as both File as well as Folder browser. You have two choices go for a third party tool like the one you found second make your own control. Surprisingly you might not find creating a very simple version of your own control very difficult.
I have gotten a program Im working on to load some pictures and display them within a listview after using a openfiledialog. What I am looking to do now is take this one step further and auto-load the images from a directory 'icons' within the application directory. Im not too sure how to go about it, So Im going to paste my current code here, and work it from there...
private void loadImageLibraryToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.Multiselect = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if (openFileDialog1.FileNames != null)
{
for (int i = 0; i < openFileDialog1.FileNames.Length; i++)
{
addImage(openFileDialog1.FileNames[i]);
}
}
else
addImage(openFileDialog1.FileName);
}
}
private void addImage(string imageToLoad)
{
if (imageToLoad != "")
{
imageList1.Images.Add(Image.FromFile(imageToLoad));
listView1.BeginUpdate();
listView1.Items.Add(imageToLoad, baseValue++);
listView1.EndUpdate();
}
}
Edit to Clarify: The code provided shows how to load and show the images in a listview control. What Im looking to do now is upon starting the app, load the images automatically from a folder in the programs directory, and then display them in the listview.
Off the top of my head with no IDE so there may be mistakes! try this
var files = System.IO.Directory.GetFiles(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\icons")
files will be an array of strings containing all the files in the directory which you can then loop as you have above using the array
openFileDialog1.FileNames
The \ may not be required before icons, I can't remember if GetDirectoryName drops the trailing \ from the path or not.
you can also pass a filter to GetFiles to only return certain file types.
HTH
EDIT: I have edited the code above to use
System.Windows.Forms.Application.ExecutablePath
rather than
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase
On testing the code now I have access to an IDE it seems the CodeBase property prepends the path with file:/// which caused my IDE to blow up with an error saying
URI formats are not supported
the code now works in my IDE, You need to make sure your icons Directory is in the same directory as your executable so in my case ....bin\debug\
Give this a try and if it still fails let me know!
I have a C# .NET 3.5 app that I have incorporated the DragDrop event on a DataGridView.
#region File Browser - Drag and Drop Ops
private void dataGridView_fileListing_DragDrop(object sender, DragEventArgs e)
{
string[] fileList = e.Data.GetData(DataFormats.FileDrop) as string[];
foreach (string fileName in fileList)
{
//logic goes here
}
}
My question is, how can I differentiate a windows shortcut from an actual file? I tried:
File.exists(fileName)
in an IF block which is useful to filter out directories that have been dragged in, however shortcuts get through. Is there anyway on to tell a shortcut in the data passed in by the event data, or by querying the file system once I have the name?
A Windows shortcut is a file, just with a .lnk extension.
Could you elaborate more about what you hope to do or not do with it?
If you need to go further and process the files or folders the shortcut is targeting, you might want to look at this http://www.codeproject.com/KB/dotnet/shelllink.aspx.
The project shows how to use Windows Scripting Host to manipulate shortcuts. For example, after creating a runtime callable wrapper (IWshRuntimeLibrary.dll) and adding this to your project, you can get the target of the shortcuts like this...
string targetPath;
if (System.IO.Path.GetExtension(path) == ".lnk"){
try{
IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell();
IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(path);
targetPath = shortcut.TargetPath;
}
catch { }
}
I have a program I have written in C#, which loads an image with Image.FromFile, and it loads the image successfully every time. However, when you drag and drop another file on the executable, like you are giving the program the command line argument of the file, and the file is not in the same folder as the executable, the program crashes because it says the path to the file does not exist, even though it does.
I think that by dropping a file on the executable, it's changing the path it's loading images from somehow. How can I fix this problem?
Your program would be started with a different Environment.CurrentDirectory. Always make sure you load files with an absolute path name (i.e. don't use Image.FromFile("blah.jpg")).
To get the absolute path to a file that's stored in the same directory as your EXE, you could use Application.StartupPath for example. Or Assembly.GetEntryAssembly().Location if you don't use Windows Forms.
It depends on how you are initiating the file drag outside of your application. If you click and drag a file from Windows Explorer, the full absolute path name is included in the drop. In this case the following code shows the filename and performs a drop of the file's contents into a textbox:
private void textBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
}
private void textBox1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
var objPaths = (string[])(e.Data.GetData(DataFormats.FileDrop));
if (objPaths != null && objPaths.Length > 0 && File.Exists(objPaths[0]))
{
MessageBox.Show(string.Format("Filename: {0}", objPaths[0]));
using (TextReader tr = new StreamReader(objPaths[0]))
textBox1.Text = tr.ReadToEnd();
}
}
}
So let us know more about your drag source. Most likely you will have to modify your source to drag the absolute path, or somehow determine the full path from the relative path in the drag data.
Also, your program should never crash due to bad data. Either check for the required conditions, or use a try/catch block around the necessary code.