Open Folder , Select the file using C# / WPF [closed] - c#

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I would like to know what is the best way to explore windows folder , select one then save it your project .
I am building an application to edit text , I would like to save it in a folder , save it in specific format .
Update:
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
After opening the folder , I would like to select it and get the directory .

Try out OpenFileDialog and SaveFileDialog.
StreamWriter and StreamReader are pretty useful, too!
[EDIT:] Take a look at FolderBrowserDialog to select folders.
This code should return you the path of a folder:
public string GetFolderPath()
{
FolderBrowserDialog Dialog = new FolderBrowserDialog();
while (Dialog.ShowDialog() != DialogResult.OK)
{
Dialog.Reset();
}
return Dialog.SelectedPath;
}

You have several options, unfortunately I'm not entirely sure what you've tried. You can accomplish this in a very basic way such as:
using(FolderBrowserDialog fd = new FolderBrowserDialog())
{
DialogResult result = fd.ShowDialog();
if(result == DialogResult.OK)
{
MessageBox.Show(fd.SelectedPath.ToString());
}
}
The above code will actually load the FolderBrowserDialog once a selection is made it will post it into a MessageBox. Obviously you can point or map it anywhere you'd like, this is a very basic example.

if you are using visual c# you could use the built in file browser to do such activity. However, there is no code posted so I'm not sure what you have tried.

Related

How can I display an error which prevents the OpenFileDialog class from closing in C#? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am creating a basic drawing program in which the user can open projects by clicking on a certain .png file in the class. I am using the System.Windows.Forms.OpenFileDialog class.
How can I make it so the Dialog shows an error, without closing, if the user tries to open the wrong file?
For example: I want the user to click on the file "name.png" but the user clicks on a file named "picture.png". Now an error shows up asking the user to find the "name.png" and doesn't close the dialog.
Here is my code:
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "*.png Files|*.png|All files|*.*";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
if (openFileDialog.FileName.EndsWith(".png"))
{
if(openFileDialog.FileName.Equals("name.png"))
{
//name.png found
}
else
{
// it was the wrong file!
}
}
}
Instead of checking the results afterwards, set the filter to be Name file|name.png.
This way, the user is only able to select the file you desire, and there's no need for the additional checks.

OpenFileDialog open folder with exe [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How can I promptly open application's folder using OpenFileDialog ?
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
...........
}
Guessing that you mean "Show the OpenFileDialog starting in my application's folder", just set the OpenFileDialog.InitialDir to your application's folder before showing the OpenFileDialog.
string AppPath = Path.GetDirectoryName(Application.ExecutablePath);;
openFileDialog1.InitialDir = AppPath;
If you need help finding your application's directory, see Getting root folder of application
Use the FileDialog.InitialDirectory Property if you wish to override one of the default ways for its value getting set (described on MSDN).
openFileDialog1.InitialDirectory = #"C:\"; // based on comment of question

How to create a folder in c# windows forms [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How to create a folder automatically when a solution is installed in the computer i.e( Local Disk:D) using c# windows forms?
Solution#1:
On first run of the application (make an xml file to track the first execution) you may create folder.
Solution#2: (Good one)
You may check if that directory exists ,if not then create the directory
try
{
// If the directory doesn't exist, create it.
if (!Directory.Exists(palettesPath))
{
Directory.CreateDirectory(palettesPath);
}
}
catch (Exception)
{
// Fail silently
}
Source:
check this link
How to Add Items to a Deployment Project
http://msdn.microsoft.com/en-us/library/z11b431t.aspx
Specifically:
How to: Add and Remove Folders in the File System Editor
http://msdn.microsoft.com/en-us/library/x56s4w8x.aspx
See the MSDN for more info, but the gist is:
You set the path like either of the ways below. See string literals for more info on this:
string newPath = #"c:\yourpath";
or
string newPath = "c:\\yourpath\\morepath";
To create the directory, you use this:
System.IO.Directory.CreateDirectory(newPath);
Hope this helps!

Open a file in c# [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm trying to read all the files in a folder. If the name is index.html, nothing happens. It's not even opening the file in the browser.
This is the code I use .
foreach (System.IO.FileInfo thefile in fiArr)
{
if (thefile.Name == "index.html")
{
FileStream fileStream = new FileStream(path + thefile.Name, FileMode.Open, FileAccess.Read);
}
}
All that your code this is create a FileStream pointing to this file. So you could read the file and fetch its contents in memory. But you cannot expect it to open in any browser. You could use the Process.Start method to open the file using the default program that is associated with this file type:
foreach (System.IO.FileInfo thefile in fiArr)
{
if (thefile.Name == "index.html")
{
Process.Start(thefile.Name);
}
}
Your code is putting the contents of the file into a FileStream so you can use it in your code. You'll need to do something with that FileStream next.
If you wanted the file opened using the default application (i.e. appearing in a browser) use this:
System.Diagnostics.Process.Start(thefile);

The C# equivalent [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I am trying to close the program :HHTCntrl.exe as if the user clicked exit on that program. I have the following Borland C++ code. What is the C# equivalent?
FILE *sf;
AnsiString ClosePollControl = AnsiString("HHT")+cbHHTNo->Text+AnsiString(".cls");
sf = fopen(ClosePollControl.c_str(),"w");
fclose(sf);
You're opening the file for writing. Assuming that cbHHTNo is the string containing file name, in C# it will go like that:
var path = "HHT" + cbHHTNo + ".cls";
using (var file = File.OpenWrite(path))
{
// do sth with the open file stream here
}
Well, it is actually pretty simple.
There isn't a concept of an AnsiString in C# so you have to use a string.
string ClosePollControl = "HHT" + cbHHTNo->Text + ".cls";
// Open the stream and write to it.
using (FileStream fs = File.OpenWrite(ClosePollControl))
{
}
This assumes you have some user level control, cbHHTNo, that you are getting the Text (e.g. string).
If you are trying to shutdown the process then use System.Diagnostics.Process class. For example,
Process process = Process.GetProcessesByName("HHTCntrl.exe").FirstOrDefault();
if (process != null)
{
process.Kill(); // to immediatelly kill the process or use process.CloseMainWindow() to gracefully "kill" the process
}
If you want to close an active form use Close() method.

Categories