How to handle Word close event from WinForms application? - c#

I have a WinForms application and I am looking for a way to do the following:
Click link to create a Word document from a BLOB in the database and open it.
Block the WinForms application until Word is closed.
Handle when Word is closed, check if the document was changed, and persist changes to the database.
The problem I am having is not creating the Word document and opening it, but it is hooking to the Word process to know when Word has closed. Is there some libraries to look at for doing this or any tutorials that would show how to accomplish this task?
Please see accepted solution, but here is the code I used to complete my task:
protected static string FilePath { get; set; }
public static void DisplayDocument(byte[] documentBytes, string filePath)
{
FilePath = filePath;
if (documentBytes == null) return;
if (!Directory.Exists(TEMP_FILE_DIRECTORY))
Directory.CreateDirectory(TEMP_FILE_DIRECTORY);
if (File.Exists(FilePath)) File.Delete(FilePath);
try
{
FileStream fs = new FileStream(FilePath, FileMode.Create);
fs.Write(documentBytes, 0, Convert.ToInt32(documentBytes.Length));
fs.Seek(0, SeekOrigin.Begin);
fs.Close();
ProcessStartInfo psi = new ProcessStartInfo(FilePath);
Process process = Process.Start(psi);
process.EnableRaisingEvents = true;
process.Exited += process_Exited;
process.WaitForExit();
}
catch (Exception e)
{
MessageHandler.Show(e.Message, Strings.ErrorOpeningFile);
}
}
private static void process_Exited(object sender, EventArgs e)
{
FileInfo fileInfo = new FileInfo(FilePath);
if(fileInfo.CreationTime.CompareTo(fileInfo.LastWriteTime) < 0)
Debug.WriteLine("File updated, perform database upload here.");
}

You can wait for a process to close using the following code:
process.WaitForExit();
when word will close, you can check if the file is modified and store it in database.

Related

C# Waiting for a file to be closed by an external application

Is there an Event I can capture for when a known file has been closed by an external application?
For example, a user is editing a workbook in Excel and I want to read that file as soon as the user finishes working on it and closes the file.
My current solution is to use a combination of FileSystemWatcher and Timer. The FileSystemWatcher will detect when changes have been made to a file, and start a new thread running a Timer to check when the file has closed (via try-catch) However I don't feel as though this is a good solution. If the user forgot to close the file and heads home for the weekend, it feels wasteful for my Timer to be running the whole time. If I increase the interval on my Timer, then my program won't be as responsive. Is there a solution that doesn't involve polling?
EDIT: updated with code example of what I have
private System.Windows.Forms.Timer processTimer;
private string blockedFile;
// Starts here. File changes were detected.
private void OnFileSystemWatcher_Changed(object source, FileSystemEventArgs e)
{
FileSystemWatcher fsw = (FileSystemWatcher)source;
string fullpath = Path.Combine(fsw.Path, fsw.Filter);
StartFileProcessing(fullpath);
}
private void StartFileProcessing(string filePath)
{
if (isFileOpen(new FileInfo(filePath)))
{
blockedFile = filePath;
processTimer = new System.Windows.Forms.Timer();
processTimer.Interval = 1000; // 1 sec
processTimer.Tick += new EventHandler(processTimer_Elapsed);
processTimer.Enabled = true;
processTimer.Start();
}
else
ProcessFile(filePath);
}
private void ProcessFile(string filePath)
{
// Do stuff, read + writes to the file.
}
// GOAL: Without polling, how can I get rid of this step just know right away when the file has been closed?
private void processTimer_Elapsed(object sender, EventArgs e)
{
if (isFileOpen(new FileInfo(blockedFile)) == false)
{
// The file has been freed up
processTimer.Enabled = false;
processTimer.Stop();
processTimer.Dispose();
ProcessFile(blockedFile);
}
}
// Returns true if the file is opened
public bool isFileOpen(FileInfo file)
{
FileStream str = null;
try
{
str = file.Open(FileMode.Open, FileAccess.Read, FileShare.None);
}
catch (IOException)
{
return true;
}
finally
{
if (str != null)
str.Close();
}
return false;
}

C# WPF FileSaving Exception encountered

My issue is that I keep seeing a recurring theme with trying to allow my Notepad clone to save a file. Whenever I try to save a file, regardless of the location on the hard disk, the UnauthorizedAccess Exception continues to be thrown. Below is my sample code for what I've done, and I have tried researching this since last night to no avail. Any help would be greatly appreciated.
//located at base class level
private const string fileFilter = "Text Files|*.txt|All Files|*.*";
private string currentPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
private void MenuFileSaveAs_Click(object sender, RoutedEventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.DefaultExt = "*.txt";
sfd.Filter = fileFilter;
sfd.AddExtension = true;
sfd.InitialDirectory = currentPath;
sfd.RestoreDirectory = true;
sfd.OverwritePrompt = true;
sfd.ShowDialog();
try
{
System.IO.File.WriteAllText(currentPath,TxtBox.Text,Encoding.UTF8);
}
catch (ArgumentException)
{
// Do nothing
}
catch(UnauthorizedAccessException)
{
MessageBox.Show("Access Denied");
}
}
Change the following lines.
...
if (sfd.ShowDialog() != true)
return;
try
{
using (var stream = sfd.OpenFile())
using (var writer = new StreamWriter(stream, Encoding.UTF8))
{
writer.Write(TxtBox.Text);
}
}
...
I hope it helps you.
You need to get the correct path context and file object from the dialog box once the user has hit 'ok'. Namely verify the user actually hit ok and then use the OpenFile property to see what their file selection is:
if (sfd.ShowDialog.HasValue && sfd.ShowDialog)
{
if (sfd.OpenFile() != null)
{
// convert your text to byte and .write()
sfd.OpenFile.Close();
}
}

Creating a text file with user entered name using c#

hi i have a webform where i am trying to create a txt file in a particular directory but i want to the name of the txt file to be entered by the user but I am not getting it how to do it please help
the code below creates a text file with name NameBox.Text.ToString I dont want that please help and thanks.
protected void Page_Load(object sender, EventArgs e)
{
NameLabel.Visible = false;
NameBox.Visible = false;
submit.Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
NameLabel.Visible = true;
NameBox.Visible = true;
submit.Visible = true;
}
protected void submit_Click(object sender, EventArgs e)
{
// string fname = NameBox.Text;
string path = #"D:\NameBox.Text.ToString.txt";
try
{
// Delete the file if it exists.
if (File.Exists(path))
{
// Note that no lock is put on the
// file and the possibility exists
// that another process could do
// something with it between
// the calls to Exists and Delete.
File.Delete(path);
}
// Create the file.
using (FileStream fs = File.Create(path))
{
Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
// Add some information to the file.
fs.Write(info, 0, info.Length);
}
// Open the stream and read it back.
using (StreamReader sr = File.OpenText(path))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
catch (Exception Ex)
{
Console.WriteLine(Ex.ToString());
}
}
}
First, keep in mind that your IIS will not allow you to access your local file System as you expcect in a "console" application, maybe running with local admin rights.
Second, be sware of illegal characters for file names, you need to check or replace it.
And third, what if two users decide to use the same filename?
As Dejan Dakic already mentioned, step back and reconsider, maybe about using a database, like SQLite or something else.
string path = #"D:\NameBox.Text.ToString.txt";
That won't work. if your textfield is called "NameBox" you need to access the value of its text which is not possible within a String.
Try this:
String path = #"D:\" + NameBox.Text + ".txt";
If you're new to programming, I'd suggest to go get a book or some tutorials and get started that way! :)

How come FileSystemWatcher doesn't fire events, but does with SMSTrace on?

I create a FileSystemWatcher object to monitor a log file. It's initialized to listen to all possible events (lastwrite, lastaccess, etc..) but it doesn't fire events when the file is being written to.
however, if I open SMSTrace and listen to that file with it (and clearly see that the file is updated constantly) the filesystemwatcher DOES fire events.
What does SMSTrace do to the file?
how can this be explained and how can I fix it?
This is the code:
private FileSystemWatcher fileWatcher;
private FileStream fileStream;
private StreamReader streamReader;
private String fullPath = null;
private String dir = null;
private String fileName = null;
private void selectLogFile()
{
// TODO: try to restore previous settings before asking the user for the log file
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = "C:\\";
openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
fullPath = openFileDialog.FileName;
dir = Path.GetDirectoryName(fullPath);
fileName = Path.GetFileName(fullPath);
}
}
// TODO: what to do when file erases? (reboot) - clear the window?
public LogListener()
{
try
{
Thread selectFileThread = new Thread(new ThreadStart(selectLogFile));
selectFileThread.SetApartmentState(ApartmentState.STA);
selectFileThread.Start();
selectFileThread.Join();
// The user did not select a file - nothing to do
if (fullPath == null)
{
return;
}
// Create file listener to listen on changes to log
fileWatcher = new FileSystemWatcher(dir);
// Create a file stream to read the data from the log file
fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
// Create a stream reader from the fileStream to read text easily
streamReader = new StreamReader(fileStream);
// Watch for changes in LastWrite
fileWatcher.NotifyFilter = NotifyFilters.LastWrite;
// Only watch the log file.
fileWatcher.Filter = fileName;
// Add event handlers.
fileWatcher.Changed += new FileSystemEventHandler(OnChanged);
// Initial syncing of the file
readFile();
// Begin watching for events
fileWatcher.EnableRaisingEvents = true;
Log.Add("Started");
}
catch (Exception e)
{
Log.Add("Exception: " + e.Message);
}
}
void OnChanged(object source, FileSystemEventArgs e)
{
readFile();
}
public void readFile()
{
String line;
String bytesString = "";
Log.Add(DateTime.Now+":readFile()...");
// Some more code here...
}
This is a long shot, since you haven't provided any code, but...
Did you remember to set the EnableRaisingEvents property to true on your FileSystemWatcher? e.g.:
var w = new FileSystemWatcher("path");
w.Created += DoSomething;
w.Changed += DoSomething;
w.EnableRaisingEvents = true; // No events raised until you do this!
EDIT: OK, you're already doing this. Please disregard this answer, I have no idea what's going on :P
Something to look for is your code removing, then re-adding the folder you're watching after you created the flesystemwatcher. If it does that, then the watcher becomes invalid and you don't get any events raised.
I had a bug in my code doing just that today and figured it out after reading this post.

In C# while creating a file second time will throw an exception

I am creating a file using file stream, but before that i am applying if condition to see if the file exist or not. When i click on button and if supppose file is there it deletes the file. Its ok, and again if i press the button the file gets created. At first time it works well.
Now the file is created, again if I press the button and it should delete but it is trhowing an exception saying that*The process cannot access the file 'C:\Hello1' because it is being used by another process.*
Below is my code
private void button2_Click(object sender, EventArgs e)
{
string fileName = #"C:\Hello1";
if
(File.Exists(fileName))
{
File.Delete(fileName);
MessageBox.Show("File is deleted");
}
else
{
FileInfo createFile = new FileInfo(fileName);
FileStream create = createFile.Create();
MessageBox.Show("Created");
}
}
So why I am not able to delete second time, My text file is not open also but still it is showing the exception.
You're never closing your stream that created the file. Put your FileStream in a using statement, which will automatically clean up the open file handle.
using(FileStream create = createFile.Create())
{
//code here
}
The file stream is still opened when you're trying second time, try this:
private void button2_Click(object sender, EventArgs e)
{
string fileName = #"C:\Hello1";
if
(File.Exists(fileName))
{
File.Delete(fileName);
MessageBox.Show("File is deleted");
}
else
{
FileInfo createFile = new FileInfo(fileName);
using(FileStream create = createFile.Create())
{
MessageBox.Show("Created");
}
}
}
Oh yes i got the answer,
I need to use
private void button2_Click(object sender, EventArgs e)
{
string fileName = #"C:\Hello1";
if
(File.Exists(fileName))
{
File.Delete(fileName);
MessageBox.Show("File is deleted");
}
else
{
FileInfo createFile = new FileInfo(fileName);
FileStream create = createFile.Create();
MessageBox.Show("Created");
create.Close();
}
}
We need to use create.Close();
Here is an example I used to write a local log:
StreamWriter log;
string fpath = string.Format(#"{0}\{1}.txt",GetDirectory(),DateTime.Now.ToString("yyy-MM-dd"));
if (!File.Exists(fpath))
{
log = new StreamWriter(fpath);
}
else
{
log = File.AppendText(fpath);
}
log.WriteLine(string.Format("{0} ==> {1}",DateTime.Now.ToString("MM/dd/yyy HH:mm:ss"), Message));
log.Dispose();
log = null;

Categories

Resources