open pdf file selected in listbox - c#

I have all the dirctories (2014, 2012), the files of each selected folder (.pdf) in the listbox 2
I get the dirctories by this code
if (FBD.ShowDialog() == DialogResult.OK)
{
listBox1.Items.Clear();
DirectoryInfo[] diri_info = newDirectoryInfo(FBD.SelectedPath).GetDirectories();
foreach (DirectoryInfo diri in diri_info)
{
listBox1.Items.Add(diri);
}
and i get the files by this code
private void button1_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex >= 0)
{
DirectoryInfo dirictory_choisis = (DirectoryInfo)listBox1.SelectedItem;
FileInfo[] files = dirictory_choisis.GetFiles();
listBox2.Items.Clear();
foreach (FileInfo file in files)
{
listBox2.Items.Add(file);
}
}
else
{
MessageBox.Show("selectioner un dossier");
}
}
Now how I can open the selected file (.pdf) ?
i use this code but dosn't work ( throw an exception file dosn't found)
private void listBox2_Click(object sender, EventArgs e)
{
FileInfo file = (FileInfo) listBox2.SelectedItem;
Process.Start(file.Name);
}

There is a syntax error in your code: "newDirectoryInfo"
By the way, file.Name only returns the name (not including path). You should replace that line with:
Process.Start(file.FullName);
So the listBox2_Click should be like this:
private void listBox2_Click(object sender, EventArgs e)
{
FileInfo file = (FileInfo)listBox2.SelectedItem;
Process.Start(file.FullName);
}

Related

How to save and load to preferences Windows Forms

I am making program, which copy files from directory to other directories in Windows Forms C#. But I tried save and load directory path to Preferences > Settings and its not working. Copying, deleting its already good, only this saving and loading doesnt work. I want do this: user can in textbox write path to source and target directory and save it. That when user want copy files, he dont need write path again and again because program save path in this preferences.
Form:
There are source path and target path for directory. Then "Save values" should save this paths as string. And "Save backup" copy files. Text_box1 is textbox next to "Source path" and text_box2 is "Target path"
Here is code:
namespace AutoSave
{
public partial class Form1 : Form
{
string sourcePath;
string targetPath;
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
var dir = new DirectoryInfo(targetPath);
if (dir.Exists)
{
DeleteDirectory(targetPath);
CopyFiles();
}
else
{
CopyFiles();
}
}
private void CopyFiles()
{
try
{
System.IO.Directory.CreateDirectory(targetPath);
var dir1 = new DirectoryInfo(sourcePath);
foreach (FileInfo file in dir1.GetFiles())
{
string targetFilePath = Path.Combine(targetPath, file.Name);
file.CopyTo(targetFilePath);
}
label2.Text = "Save is successful";
}
catch
{
label2.Text = "Something is wrong";
}
}
public static void DeleteDirectory(string target_dir)
{
string[] files = Directory.GetFiles(target_dir);
string[] dirs = Directory.GetDirectories(target_dir);
foreach (string file in files)
{
File.SetAttributes(file, FileAttributes.Normal);
File.Delete(file);
}
foreach (string dir in dirs)
{
DeleteDirectory(dir);
}
Directory.Delete(target_dir, false);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
//SAVE PATHS
private void button2_Click(object sender, EventArgs e)
{
sourcePath = textBox1.Text;
targetPath = textBox2.Text;
Properties.Settings.Default.SourcePath = targetPath;
Properties.Settings.Default.TargetPath = sourcePath;
Properties.Settings.Default.Save();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
Have I bad code or what I did bad please? Have you some tips? In advance I am sorry for my English i tried my best :D Thanks for help.

Stuck with WMP in C#

I want to fetch 5 media files from a Directory [not with dialog box] and play with axwindowsmediaplayer in C# one after one. Songs to be played as playlist.
I am new to C#.
private void Form1_Load(object sender, EventArgs e)
{
/*
foreach(string dir in Directory.GetDirectories(#"E:\\Project"))
{
DirectoryInfo info = new DirectoryInfo(dir);
this.listBox1.Items.Add(info.Name);
}
foreach(string file in Directory.GetFiles("E:\\Project\\Surprise"))
{
FileInfo FileInfo = new FileInfo(file);
this.listBox1.Items.Add(FileInfo.Name);
}
*/
string path = #"E:\\Project\\Surprise";
foreach (string s in Directory.GetFiles(path, "*.mp4").Select(Path.GetFileName))
listBox1.Items.Add(s);
listBox1.SelectedItems.Clear();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = listBox1.SelectedItem.ToString();
axWindowsMediaPlayer1.Ctlcontrols.play();
}

Using a ComboBox selection as directory source for another ComboBox, C#

So I have created a form that creates both a .txt file and a directory with the same name under the directory C:\Modules.
I have made it possible to select the .txt files from ModuleSelectorComboBox and now what I am having trouble getting to work is using the name of the file selected in ModuleSelectorComboBox to form part of the name of the directory in NoteSelectorComboBox.
public Default()
{
InitializeComponent();
System.IO.Directory.CreateDirectory(#"C:\Modules");
string[] files = Directory.GetFiles(#"C:\Modules");
foreach (string file in files)
ModuleSelectorComboBox.Items.Add(Path.GetFileNameWithoutExtension(file));
}
private void moduleToolStripMenuItem_Click(object sender, EventArgs e)
{
NewModule newmodule = new NewModule();
newmodule.Show();
}
private void ModuleSelectorComboBox_SelectedValueChanged(object sender, EventArgs e)
{
richTextBox1.Clear(); //Clears previous Modules Text
string ModulefileName = (string)ModuleSelectorComboBox.SelectedItem;
string filePath = Path.Combine(#"C:\Modules\", ModulefileName + ".txt");
if (File.Exists(filePath))
richTextBox1.AppendText(File.ReadAllText(filePath));
else
MessageBox.Show("There's been a problem. Please restart the program. \nError 1", "Error 1", //error 1 is file deleted while the program is running
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
}
private void button1_Click(object sender, EventArgs e)
{
ModuleSelectorComboBox.Items.Clear();
string[] files = Directory.GetFiles(#"C:\Modules");
foreach (string file in files)
ModuleSelectorComboBox.Items.Add(Path.GetFileNameWithoutExtension(file));
}
private void NoteSelectorComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
string ModulefileName = (string)ModuleSelectorComboBox.SelectedItem;
string[] files = Directory.GetFiles(#"C:\Modules\" + ModulefileName); //THIS IS WHAT I HAVE TRIED BUT CANNOT GET TO WORK
foreach (string file in files)
NoteSelectorComboBox.Items.Add(Path.GetFileNameWithoutExtension(file));
}
}
So if 'Module 1' is selected in the ModuleSelectorComboBox the directory listing for NoteSelectorComboBox will be set to C:\Modules\Module 1 (ie. C:\Modules\<NAME OF SELECTED MODULE From ModuleSelectorComboBox> and so the files in that folder would be shown in the ComboBox.
I have created an OnClick(); event that has now solved this issue.
private void button2_Click(object sender, EventArgs e)
{
string fileName = (string)ModuleSelectorComboBox.SelectedItem;
NoteSelectorComboBox.Items.Clear();
string[] files = Directory.GetFiles(#"C:\Modules\" + (string)ModuleSelectorComboBox.SelectedItem);
foreach (string file in files)
NoteSelectorComboBox.Items.Add(Path.GetFileNameWithoutExtension(file));
}

how to find that whether the file exist or not using c#

private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog FldrBrowseDlg = new FolderBrowserDialog();
FldrBrowseDlg.ShowNewFolderButton = true;
DialogResult DigRslt = FldrBrowseDlg.ShowDialog();
if (DigRslt.Equals(DialogResult.OK))
{
textBox1.Text = FldrBrowseDlg.SelectedPath;
Environment.SpecialFolder rootfolder = FldrBrowseDlg.RootFolder;
}
}
private void button2_Click(object sender, EventArgs e)
{
DirectoryInfo dir = new DirectoryInfo(textBox1.Text);
FileInfo[] files = dir.GetFiles("*doc.zip", SearchOption.AllDirectories);
foreach (FileInfo fl in files)
{
string s1 = fl.ToString();
string name = s1.Substring(0, 28);
string kyrname = name + ".txt";
if (File.Exists(textBox1.Text+"*/"+kyrname))
{
label1.Text = "have kyrplus";
}
else
{
listBox1.Items.Add(name);
}
I want to search the file but it is not taking the the path that I am giving in File.Exixts() function what to do?
Thats probably because your path is not correct.
File.Exists(textBox1.Text+"*/"+kyrname)
You need to check what is the value in your textBox1.Text and then you can concatenate the value with "*/" and .txt. It doesnt look to me as a valid path.
EDIT:
You can try like this if you are looking for a file in all the subdirectory
var file = Directory.GetFiles(textBox1.Text, kyrname, SearchOption.AllDirectories)
.FirstOrDefault();
if (file == null)
{
// File does not exist
}

How can i get from a listView1 item(file) the file full path?

I have this in my designer:
On the right is the files in a listView1.
On the left is the directory main directory of this files treeView1.
I have this code in menu strip item clicked event :
void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem.Text == "Upload")
{
List<String> selected = new List<String>();
foreach (ListViewItem lvi in listView1.SelectedItems)
{
selected.Add(lvi.Text);
}
AllFiles = selected.ToArray();
Bgw.RunWorkerAsync();
}
}
The problem is that the file/s in AllFiles array are only the filenames for example: bootmgr or install.exe
But i need that in the All Files each file will have also it's full path for example:
c:\bootmgr or c:\install.exe or c:\test\test\example.txt
How can i add to AllFiles also the paths ?
I tried now:
void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem.Text == "Upload")
{
List<String> selected = new List<String>();
string dir = treeView1.SelectedNode.FullPath;
foreach (ListViewItem lvi in listView1.SelectedItems)
{
string g = Path.Combine(dir, lvi.Text);
selected.Add(g);
}
AllFiles = selected.ToArray();
Bgw.RunWorkerAsync();
}
}
And in form1:
private void FtpProgress_DoWork(object sender, DoWorkEventArgs e)
{
f = new FtpSettings();
f.Host = "ftP://ftp.newsxpressmedia.com";
f.Username = "...";
f.Password = "...";
files = TV_LV_Basic.ExplorerTree.AllFiles;
StringArrayUploadFiles(sender, e);
}
AllFiles contain the files and paths for example C:\test.txt
Then :
private void StringArrayUploadFiles(object sender, DoWorkEventArgs e)
{
try
{
foreach (string txf in files)
{
string fn = txf;
BackgroundWorker bw = sender as BackgroundWorker;
if (f.TargetFolder != "" && f.TargetFolder != null)
{
createDirectory(f.TargetFolder);
}
else
{
f.TargetFolder = Path.GetDirectoryName(txf);
//createDirectory(f.TargetFolder);
}
string UploadPath = String.Format("{0}/{1}{2}", f.Host, f.TargetFolder == "" ? "" : f.TargetFolder + "/", Path.GetFileName(fn));
Now in txf for example i have C:test.txt
Then in f.TargetFolder i have: C:
Then in UploadPath i have: ftp://ftp.newsxpressmedia.com/C:/eula.1031.txt
But instead C: i need it to look like: ftp://ftp.newsxpressmedia.com/C/eula.1031.txt
And there are sub directories for example then: ftp://ftp.newsxpressmedia.com/C/Sub/Dir/eula.1031.txt
In the menuStrip1_ItemClicked event when i select a file for example test.txt already in this event i did a mess.
FileInfo fi = new FileInfo("temp.txt");
Determine the full path of the file just created.
DirectoryInfo di = fi.Directory;
Figure out what other entries are in that directory.
FileSystemInfo[] fsi = di.GetFileSystemInfos();
to display directoryinfo fullname in console
Console.WriteLine("The directory '{0}' contains the following files and directories:", di.FullName);
Print the names of all the files and subdirectories of that directory.
foreach (FileSystemInfo info in fsi)
Console.WriteLine(info.Name);
Here

Categories

Resources