private void btnNew_Click(object sender, System.Windows.RoutedEventArgs e)
{
OpenFileDialog of = new OpenFileDialog();
of.Multiselect = true;
of.Filter = "JPG Dosyaları|*.jpg|JPEG Dosyaları|*.jpeg";
of.ShowDialog();
foreach (var file in of.Files)
{
MessageBox.Show(file.FullName);
}
}
The problem is i want to open multiple files' in Silverlight and i don't know any other way doing it than passing the filenames into a foreach loop. The problem is Silverlight don't like if i try to reach files in a loop, it must be a direct command from user.
In this case it throws an exception:
File operation not permitted
So is there another way manipulating local files (not from isolated space), or is there any way i can make this code work?
Thanks guys.
Don't use file.FullName to open the file. You get a FileInfo object back, use one of its OpenXxxx() methods to open the file.
Related
I am trying to save all contents in a ListBox to a .txt file. I have the following code:
private void btn_Save_Click(object sender, EventArgs e)
{
const string sPath = "save.txt";
System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(sPath);
SaveFile.WriteLine(listBox1.Items);
SaveFile.ToString();
SaveFile.Close();
MessageBox.Show("Programs saved!");
}
But when I test it the code works but and it acts like it saved the file but when I go into my File Explorer, I cannot see the file anywhere.
Does anyone have any answers
You need to provide the explicit path. Example:
const string sPath = #"C:\MyFolder\save.txt";
What u have to do?
First:
You need a StreamWriter, u done it right. This StreamWriter needs a Path, you did this too.
Second:
You want all Items of your listbox, so use foreach. You havent done this.
Third: Close the Stream, you done it too. Use using instead of close , its better :) do the same :)
Where i find the file? Go to your Project right in Visual Studio right click on it -> open in windows explorer -> bin -> Debug and your text file is their. If you dont give a absolute path, the saved data is always next to your .exe.
Solution:
string path = "save.txt";
using (StreamWriter sw = new StreamWriter(path))
{
foreach (string s in listBox1.Items)
{
sw.WriteLine(s);
}
};
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'm trying to make a Media Player where you can choose either to render a file from url or local disc. I had no problem making it to open and render url file
void LoadVideo_Click(object sender, RoutedEventArgs e)
{
LoadVideo.IsEnabled = false;
mediaElement.Source = new Uri(path, UriKind.Absolute);
With string path = "http://www.blablabla.com/movie.wmv"
The problem occurs when I'm trying to specify local disc file path(as "c:\movie.wmv" or #"c:\movie.wmv"). It simply doesn't work that way.
As far as I have read, you don't have direct access to files on your hard drive besides those which already are in the project directory. What I want to do is:
use Dialog Box to choose a file to open
save the path of file into string and transfer it to MediaElement.Source
Unfortunately, I don't have a clue how to do it. I would be grateful for any advices.
Here you go, this should do the trick:
OpenFileDialog fdlg = new OpenFileDialog(); //you need to use the OpenFileDialog, otherwise Silverlight will throw a tantrum ;)
fdlg.Filter = "MP4 Files|*.mp4|AVI files|*.avi"; //set a file selection filter
if (fdlg.ShowDialog() != true) //ShowDialog returns a bool? to indicate if the user clicked OK after picking a file
return;
var stream = fdlg.File.OpenRead(); //get the file stream
//Media is a MediaElement object in XAML
Media.SetSource(stream); //bread and butter
Media.Play(); //no idea what this does
Here's an extensive example on how to use the OpenFileDialog. As for the MediaElement, you can see in the code above all you needed was the SetSource() method (as opposed to the Source property).
I have a customized OpenFileDialog (VS2008, C#, Windows Forms) with a ComboBox. The ComboBox will have a list of paths which the user can select.
My question, is there a way I can change the directory in Open File Dialog to point to the path in the combobox selected item.
InitialDirectory works only before I open the dialog, I wanted a way to change the directory programatically after the dialog is open.
Thanks
If you're using Vista or Windows 7 with .NET 3.5 SP1 I recommend you use the CustomPlaces property on OpenFileDialog rather than a custom combo box.
See this MSDN article (for WPF): http://msdn.microsoft.com/en-us/library/microsoft.win32.filedialog.customplaces(v=VS.100).aspx
Or this MSDN article (for Windows Forms): http://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.customplaces(v=VS.100).aspx
On Windows 2000 and XP it is also possible to customize the places side bar. But it is more difficult and requires you to use some C++ code (via CLI/C++ is probably best). The technique is described in detail in this MSDN article: http://msdn.microsoft.com/en-us/magazine/cc300434.aspx
If you're dead set on using a combo box you've added to the OpenFileDialog then you will probably just need to know what windows message to send to the dialog. I'm afraid I don't know which message you need to send. The nasty internal Win32 API details of the Common Open/Save dialog is detailed here: http://msdn.microsoft.com/en-us/library/ms646960(VS.85).aspx
If you can figure out which messages to send to the window the probably way of doing things is to fill the filename text field with the directory you want to switch to simulate a OK button click. The dialog will switch to that directory if you do this.
Sending messages to this window will probably require you to not use OpenFileDialog directly but rather subclass the abstract FileDialog class upon which it is based.
Just set the InitialDirectory property of openFileDialog1
private void button1_Click(object sender, System.EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = cmbPath.SelectedValue.ToString();
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
openFileDialog1.FilterIndex = 2 ;
openFileDialog1.RestoreDirectory = true ;
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
// Insert code to read the stream here.
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
As already said InitialDirectory works before hand but why would you change the folder afterwords? FileOpenDialog is a modal dialog, therefore the user can't use anything else of your application than the dialog. What is the benefit and reason why you wan't to set the folder? It seems your using the wrong tools to get the job done.
If i created a button named Browse ,,If i click Browse button i have to
browse my system folders .Can any one give me the required code to browse
the specific folders
Check out the FolderBrowserDialog if you are wanting to find a folder.
If you are wanting to open a file, you can use the OpenFileDialog.
Both links provide examples of how to use the dialogs.
This MSDN link provides how to get the special system folders. And you can specify the type of special folder you want by using the appropriate enumeration. Check this link for those.
Essentially, you are going to do something like so if you want to pop up a dialog and browse to the System folder and select some files from there:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog od = new OpenFileDialog();
od.InitialDirectory = Environgment.SpecialFolder.System;
od.Multiselect = true;
if (od.ShowDialog() == DialogResult.OK)
{
// do stuff
// od.Filenames will hold the string[] of selected files
}
}
Assuming you want to display the results in a list named files something like:
String directory = Environment.GetFolderPath (Environment.SpecialFolder.System);
String[]files = Directory.GetFiles (directory);
foreach (String file in files)
files.Add (file);
You can use a FolderBrowserDialog control and call the code there if you want to browse multiple directories.