OpenFileDialog open folder with exe [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 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

Related

Open Folder , Select the file using C# / WPF [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 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.

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.

How to get No extension file type [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 want get file type but my file noting extension in C#,Asp.net.
I want get file type from downloaded file with webClient.DownloadFile.
Can trace file?
Can understand downloaded file is pdf or no?
There is no absolute way to get the file type of a file with no extension.
Some file types, such as EXE, have a header that you could check. Other files, such as text files, could probably be detecting using some sort of heuristic that looks for only text characters. But still other files would not be identifiable.
As long as you know the extension beforehand it's possible, otherwise you'd have to be familiar with the file's structure. If all you're doing is downloading a pdf without the .pdf extension, rename your file [something].pdf when you download it and you'll be able to access it as a regular pdf.
There are some links where same question has been asked . I hope it helps
https://superuser.com/questions/435224/how-do-i-find-out-the-file-type-without-an-extension
http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/16c34850-8799-4a8b-8702-2f59d96e79c1
http://www.troublefixers.com/open-files-without-any-valid-file-extension/
http://bytes.com/topic/c-sharp/answers/812692-open-file-without-knowing-extension
FileStream fs = new FileStream(#"c:\a.pdf", FileMode.Open, FileAccess.Read);
StreamReader r = new StreamReader(fs);
string pdfText = r.ReadToEnd();
Regex rx1 = new Regex(#"/Type\s*/Page[^s]");
MatchCollection matches = rx1.Matches(pdfText);
MessageBox.Show("The PDF file has " + matches.Count.ToString() + " page(s).";

File.Copy Access Denied [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 have a problem with copying from local disk to flash. The code worked before, but after I create anoteher foreach loop and I had to create new objects, the File.Copy functionality isn't working anymore.
In value File | System.IO.File there are values like: Error_Access_Denied | 5, Error_Invalid Parameter| 87, GetFileExInfoStandard | 0.
edit: locationUSB present file path on flash. (locationUSB == "D:\something.hex") and x._location == "C:\something_1.hex" .
foreach (object item in this.dataGridView2.Rows)
{
versionOnDisk = this.VersionInt(x._version);
versionOnFlash = this.VersionInt(((DataGridViewRow)item).Cells[2].Value.ToString());
if (versionOnFlash > versionOnDisk)
forbidCopying = true;
else
locationUSB = _logicalDrive + ((DataGridViewRow)item).Cells["Filename"].Value.ToString(); // <-- location value (because of the foreach)
if (!forbidCopying)
File.Copy(x._location, locationUSB, true); // <--
else if (AllowDelete.Checked)
File.Delete(locationUSB);
}
edit:
If I change the location into logical drive path, which value is "D:\" I get the DirectoryNotFoundException was unhandled: Could not find a part of the path 'D:\'.
Most probably after creating new files, you are not closing the FileStreams. Close the FileStreams of the newly created files using either myFile.Close(); or create new files inside using (var myFile = File.Create(myPath)) blocks.

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);

Categories