Checking if a file exists on your computer - c#

Basically i have a list of folder paths on XML File. I created a dataGrid View which reads this XML file and displays the list of folders in the XML into the data Grid View. Now I need to create a way where the user chooses a folder path from one of the list I have and the program will check if this folder exists on the computer being used.Is there a way to create this program?

Try Button with event handler for the click event and assuming that your input textbox is named textBoxForPath this should work:
private void Button1_Click(object sender, EventArgs e)
{
string path = this.textBoxForPath.Text;
if (File.Exists(path))
{
// Do Something
}
}

Related

How to create a ASPX file after clicking a button on another form

Hopefully this is clear enough, i have this bit of code :
private void BtnAdd_ServerClick(object sender, EventArgs e)
{
var path = #"C:\Users\User\Desktop\Demo\Demo\" + BtnName.Value;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
That creates a folder named after the value of a text box, and i need to know if there's a way to create an aspx file the same way, and save it in one of those folders or anywhere it just needs to be accessible to the project, thanks in advance! I'm open to any questions or code updates.

How to SAVE edited Treeview node text in C# Visual Studio 2010

I am trying to edit treeview node names in a treeview (they become editable on a button click ) and then I want them to remain saved (if I exit and enter the aplication again, the new, edited names should be displayed), BUT they always revert to the original name(text) on program reentering.
private void button1_Click(object sender, EventArgs e)
{
treeView1.LabelEdit = true;
}
Questions is how to make the new treeview node names be saved after editing, so when I enter the aplication again they don't reset to the old ones.
You can use the event on application shutdown to manually iterate over the TreeView and save the data to a file. Then when you applications starts again read them and populate the TreeView.
You cannot edit your project by modifying the developped program runtime.

how i can insert an image in web browser control in c# with out open image ui?

I have a web browser control in C# that it's design mode is on. I use it to make a (WYSIWYG) HTML editor. I want to insert a photo on this without UI, when user wants to insert image I show to him/her a window that shows on it some known name for user. Then with search (s)he find her/his photo and add it to control.
this is my open image form:
Photos are in database and user only know them with names. I upload all of them in a folder and show above list to user and user selects a picture. I want to add image by it's location in hard disk and allow user to set its alignment .
How I can do this ?
Adding a new element to a document is very logical. The first step is to create the node (element) you wish to append, the next is to find where you wish to append it within the document, and the final step is to actually do the appending.
I write an Example for you:
private void Form1_Load_1(object sender, EventArgs e)
{
webBrowser1.DocumentText = "<html><body></img></body></html>";
}
private void insert_image_btn_Click(object sender, EventArgs e)
{
HtmlElement userimage = webBrowser1.Document.CreateElement("img");
userimage.SetAttribute("src", "image location");
userimage.Id = "imageid";
webBrowser1.Document.Body.AppendChild(userimage);
}
private void btn_set_aliign_Click(object sender, EventArgs e)
{
webBrowser1.Document.GetElementById("imageid").SetAttribute("float", "right");
}

how to open listview window explorer in listview control

i have a listview window browser thats working completely fine .i open folder through folder browser and the files and folders in that particular directory opens in the listview via using
PopulateListView(path)
now in my mouse double click event im opening a particular file and folder it opens the file but when it opens the directory a new window pops up .i want that directory to b opened in the listview control...the code for this scenarioa is
private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
string pathdoubleClicked = listView1.FocusedItem.Tag.ToString();
PopulateListView(pathdoubleClicked);
Process.Start(pathdoubleClicked);
simpleStack.Push(pathdoubleClicked);
}
now i want to do it with if else like if the path is of drectory then go to populatelistview method other wise process.start but its now working any idea how can i do this
From what I understand, if the path is a directory, you want to display its content on double-click. If it's a file, you want to open it.
So you would simply do:
private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
string pathdoubleClicked = listView1.FocusedItem.Tag.ToString();
if (System.IO.Directory.Exists(pathdoubleClicked))
{
PopulateListView(pathdoubleClicked);
}
else
{
Process.Start(pathdoubleClicked);
}
// ?
simpleStack.Push(pathdoubleClicked);
}

Drag and drop files into WPF

I need to drop an image file into my WPF application. I currently have a event firing when I drop the files in, but I don't know how what to do next. How do I get the Image? Is the sender object the image or the control?
private void ImagePanel_Drop(object sender, DragEventArgs e)
{
//what next, dont know how to get the image object, can I get the file path here?
}
This is basically what you want to do.
private void ImagePanel_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
// Note that you can have more than one file.
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
// Assuming you have one file that you care about, pass it off to whatever
// handling code you have defined.
HandleFileOpen(files[0]);
}
}
Also, don't forget to actually hook up the event in XAML, as well as setting the AllowDrop attribute.
<StackPanel Name="ImagePanel" Drop="ImagePanel_Drop" AllowDrop="true">
...
</StackPanel>
The image file is contained in the e parameter, which is an instance of the DragEventArgs class.
(The sender parameter contains a reference to the object that raised the event.)
Specifically, check the e.Data member; as the documentation explains, this returns a reference to the data object (IDataObject) that contains the data from the drag event.
The IDataObject interface provides a number of methods for retrieving the data object that you're after. You'll probably want to start by calling the GetFormats method in order to find out the format of the data that you're working with. (For example, is it an actual image or simply the path to an image file?)
Then, once you've identified the format of the file being dragged in, you'll call one of the specific overloads of the GetData method to actually retrieve the data object in a particular format.
Additionally to answer of A.R. please note that if you want to use TextBox to drop you have to know following stuff.
TextBox seems to have already some default handling for DragAndDrop. If your data object is a String, it simply works. Other types are not handled and you get the Forbidden mouse effect and your Drop handler is never called.
It seems like you can enable your own handling with e.Handled to true in a PreviewDragOver event handler.
XAML
<TextBox AllowDrop="True" x:Name="RtbInputFile" HorizontalAlignment="Stretch" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" />
C#
RtbInputFile.Drop += RtbInputFile_Drop;
RtbInputFile.PreviewDragOver += RtbInputFile_PreviewDragOver;
private void RtbInputFile_PreviewDragOver(object sender, DragEventArgs e)
{
e.Handled = true;
}
private void RtbInputFile_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
// Note that you can have more than one file.
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
var file = files[0];
HandleFile(file);
}
}

Categories