i have a c# windows forms application that opens up a powerpoint file, now if i make a change to the powerpoint file and if my program is running it must automatically pick up the file that has been modified, or can i set like a timer to refresh the page so it will pick up the changes?
The following code works in opening a file.
private void button1_Click(object sender, EventArgs e)
{
var app = new PowerPoint.Application();
var pres = app.Presentations;
var file = pres.Open(#"C:\Pres1.pptx", MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoFalse);
PowerPoint.SlideShowSettings slideSetting = file.SlideShowSettings;
slideSetting.Run();
PowerPoint.SlideShowWindows slideShowWindows = app.SlideShowWindows;
while (true)
{
if (slideShowWindows.Count <= 0)
break;
System.Threading.Thread.Sleep(100);
}
}
You can use the FileSystemWatcher class to detect changes to that file (http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v=vs.110).aspx).
Related
I am making Office plugin to Word. Using Microsoft.Office.Interop. I have few .dotm files(Templates). When pressing a button I need to copy all text from one .dotm to my Active document. I cant figure out how can I put in variable the active document. So I can insert the info.
I tried several ways. Now I am trying to open .dotm copy text from there and then paste it to my active one. But it doesn't work. The Word will start with no errors(while starting in Visual Studio) and then when I am pressing the button it tells me that there is no open document on this: var MyDoc = app.ActiveDocument;
1)
private void Button1_Click(object sender, RibbonControlEventArgs e)
{
var app = new Microsoft.Office.Interop.Word.Application();
var MyDoc = app.ActiveDocument;
var sourceDoc = app.Documents.Open(#"C:\install\CSharp\test.docx");
sourceDoc.ActiveWindow.Selection.WholeStory();
sourceDoc.ActiveWindow.Selection.Copy();
MyDoc.ActiveWindow.Selection.Paste();
2)
var newDocument = new Microsoft.Office.Interop.Word.Document();
newDocument.ActiveWindow.Selection.Paste();
newDocument.SaveAs(#"C:\install\CSharp\test1.docx");
But if I do that way(2):
It will work. But I need to paste into my active document. Also I think that the copy paste method is not so good. Maby there is some other method to import one document into an other.
Never, ever, ever do (2). Using new Microsoft.Office.Interop.Word.Document(); is certain to cause unexpected memory leak problems. Word lets you do it, but it's not supported. new should only ever be used for a new instance of the Word application.
Instead, use Documents.Add referencing the template file. That will create a copy of the template as a new document - very simple:
private void Button1_Click(object sender, RibbonControlEventArgs e)
{
var app = new Microsoft.Office.Interop.Word.Application();
var MyDoc = app.Documents.Add(#"C:\install\CSharp\test.docx");
}
I have a PHP website on a Linux server. I made a button next to the phone numbers on the site that writes a text file on the server with that number. The following code works.
$file = './gebruikers/'.$naam.'/nummer.txt';
$write = $_POST['num'];
file_put_contents($file, $write);
Now I made a C# application with TAPI3 to call the number in that text file.
I use a FileSystemWatcher (watcher) to check the folder where php saves the text file so it makes the call everytime the file gets updated.
The following code checks which user is selected so it watches the folder of that user for the text file.
private void cbGebruikers_SelectedIndexChanged(object sender, EventArgs e)
{
if(cbGebruikers.Text != "")
{
comboBox1.Enabled = true;
button6.Enabled = true;
lblGebruiker.Visible = false;
lblTelefoon.Visible = true;
}
path = #"\\192.168.1.9\SB Alarm programma\web-sb\gebruikers\" + cbGebruikers.Text;
watcher.Path = path;
watcher.NotifyFilter = NotifyFilters.LastAccess;
watcher.Filter = "*.*";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
lbltest.Text = watcher.Path.ToString();
}
When the text file changes the following code will execute.
private void OnChanged(object sender, FileSystemEventArgs e)
{
try
{
watcher.EnableRaisingEvents = false;
telnummer = File.ReadAllText(path + "/nummer.txt");
nummer = "0" + telnummer;
this.Invoke((MethodInvoker)delegate
{
txtNummer.Text = nummer;
MakeCall(nummer);
});
}
finally
{
watcher.EnableRaisingEvents = true;
}
}
This code works, if I change the text file in the folder on my PC or on another PC that has access to the folder the application makes the call.
But if PHP changes the text file nothing happens, but the last modified date does update.
Someone got experiencewith this?
This looks like an issue with using FileSystemWatcher in a cross platform architecture. FileSystemWatcher works by opening up a connection to the remote server, whose responsibility is responding when changes happen to the specified file. Windows platforms use the Win32 ReadDirectoryChanges(), while Linux boxes use Inotify API. Because there is no interface between the two APIs, the Linux box has no way of responding to the FileSystemWatcher.
Relevant links
http://msdn.microsoft.com/en-us/library/aa365465.aspx
http://www.mono-project.com/docs/faq/technical/
Can you try to change NotifyFilter to NotifyFilters.LastWrite? Or if you want to monitor both, change to NotifyFilters.LastWrite | NotifyFilters.LastAccess.
Also if the file is created by PHP, you probably want to add an event handler to watcher.Created.
I am using C#.With this code I am trying to open a word document (WINWORD) and get notify when the specific file or file name that I have open from the software has close.
Right now if I open the x.docx file from the software and open another y.docx manualy and close the y.docx file my programs things that was the x.docx because are having the same process name WINWORD.
I have read a lot of articles but i couldn't find anything.Is it possible to do this in any way?
private void button1_Click(object sender, EventArgs e)
{
Process mydoc = new Process();
mydoc.StartInfo.FileName = "op.docx";
mydoc.StartInfo.WorkingDirectory = "C:\\";
mydoc.Start();
mydoc.WaitForExit();
notifyIcon1.Visible = true;
notifyIcon1.Icon = new System.Drawing.Icon(Path.GetFullPath(#"C:\Users\Marios\Desktop\down.ico"));
notifyIcon1.BalloonTipTitle = "waiting...";
notifyIcon1.BalloonTipText = "Word has has opened...";
notifyIcon1.ShowBalloonTip(100);
Displaynotify();
}
protected void Displaynotify()
{
try
{
var processes = Process.GetProcessesByName("WINWORD");
notifyIcon1.Visible = true;
notifyIcon1.Icon = new System.Drawing.Icon(Path.GetFullPath(#"C:\Users\Marios\Desktop\down.ico"));
notifyIcon1.BalloonTipTitle = "exited.";
notifyIcon1.BalloonTipText = "Word has exited...";
notifyIcon1.ShowBalloonTip(100);
}
catch (Exception ex)
{
}
}
Update 1 with links from the Answer
I have change my code and now i open it with this way but still i cant do what i want
private void button1_Click(object sender, EventArgs e)
{
Word.Application oWord = new Word.Application();
Microsoft.Office.Interop.Word.Document oDoc = oWord.Documents.Open("C:\\op.docx");
oWord.Visible = true;
((Word._Document)oDoc).Close();
// ((Word._Document)oDoc).Close();
//((Word._Application)oWord).Quit();
//Displaynotify();
}
You can automate Word from your C# application. In that case you will be able to check what documents are opened at the moment and handle the Close event of the Document class which is fired when a document is closed.
See How to automate Microsoft Word to create a new document by using Visual C# for more information and sample code in C#. Also you may find the C# app automates Word (CSAutomateWord) sample project helpful.
In that case there is no need to run a new process. All the job can be done using Word automation.
I created simple program for save/open practice. Made a setup and associated my program with my own datatype, called it .xxx (for practice).
I managed to Save and Open code and data from textbox but only from my program. Double click (or enter from windows-desktop) open up my WindowsForm as it is but there is an empty textbox. I want my saved file to be opened on double click in the same condition as when I open it from my program. How to set that up??
Here is the code of simple app (cant post images but it simple - got 1 label and 1 textbox with open and save buttons):
private void ButOpen_Click(object sender, EventArgs e)
{
textBox1.Text = "";
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
string data = Read(openFileDialog1.FileName);
textBox1.Text = data;
}
else
{//do nothing }
}
private string Read(string file)
{
StreamReader reader = new StreamReader(file);
string data = reader.ReadToEnd();
reader.Close();
return data;
}
private void ButSave_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Something|*.xxx";
DialogResult result = saveFileDialog1.ShowDialog();
string file = saveFileDialog1.FileName.ToString();
string data = textBox1.Text;
Save(file, data);
}
private void Save(string file, string data)
{
StreamWriter writer = new StreamWriter(file);
writer.Write(data);
writer.Close();
}
NOTE:
My similar question was marked as duplicate but it is not, and this question which was referenced as duplicate Opening a text file is passed as a command line parameter does not help me.It's not the same thing...
Just wanted to find out how to configure registry so windows understand and load data inside the file, or to file save data somehow so i can open it with double click.
So someone please help. If something is not clear I will give detailed information just ask on what point.
Thanks
MSDN has some information about this:
https://msdn.microsoft.com/en-us/library/bb166549.aspx
Basically you need to create an entry in the registry so that explorer.exe knows to launch your program when that file is activated (e.g. double-clicked).
Explorer will then pass the path to the file as an argument to your program.
can any expert help me out to solve a problem of burning a dvd using c#.net as a front end??
i need to select files from the listview in winform and then on button click i need to burn those multiple files in the dvd..
the concept is to select the multiple files from listview then on button click it should make a folder in some desired drive.. and then it should burn that complete folder in the dvd..this whole process should be performed during a single button click....
is there any way out??
the code should be compatible to use in .net2008 and windowsXP are the given codes compatible??
im using the componenet to get the dll/class lib. from (msdn.microsoft.com/en-au/vcsharp/aa336741.aspx) but its giving me error message "there are no components in d:\filepath\burncomponent.dll to be placed on the toolbox
private void button1_Click(object sender, EventArgs e)
{
XPBurnCD cd = new XPBurnCD();
cd.BurnComplete += new NotifyCompletionStatus(BurnComplete);
MessageBox.Show(cd.BurnerDrive);
DirectoryInfo dir = new DirectoryInfo(_burnFolder);
foreach (FileInfo file in dir.GetFiles())
{
cd.AddFile(file.FullName, file.Name);
}
cd.RecordDisc(false, false);
}
private void BurnComplete(uint status)
{
MessageBox.Show("Finished writing files to disc");
}
private void button2_Click_1(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.ShowNewFolderButton = false;
fbd.Description = "Please select a folder";
fbd.RootFolder = System.Environment.SpecialFolder.DesktopDirectory;
if (fbd.ShowDialog() == DialogResult.OK)
{
_burnFolder = fbd.SelectedPath;
}
else
{
_burnFolder = string.Empty;
}
}
Check out http://msdn.microsoft.com/en-au/vcsharp/aa336741.aspx
One simple approach could be to use the command line tools dvdburn and cdburn, which are belonging to XP. For example take a look at this site.
Update
Yes, it is a console application, but you can start it within a .Net Application by using the Process class. And here you should especially take a deeper look into the StartInfo property and its members, cause here you can set the parameters or redirect the output into your program to get informations about what the program is doing.