小弟初学C#,碰到一个问题,我要让winform显示指定文件夹中的PDF文件,我使用了adobe reader自带的com组件,然后当指定文件夹中的PDF文件被替换的时候,窗体中的PDF组件也随着变换,我使用了.net的filesystemwatcher控件,随后问题也就来了,当打开一个窗口时,文件显示和替换后显示都没有问题,但同时打开5个窗口时,替换文件夹中的文件,则只能打开两个,其他一个显示在载入,剩下两个则完全没有反应,总结了下,应该是filesystemwatcher下的 change 事件 同时让5个窗体一起访问了文件,导致了文件的无法载入,但我不知道解决的方法和思路,还是有相应的技术处理这种情况,(如果放在局域网,这种情况要如何解决),求各位大侠指点迷津...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;namespace PDFDisplay
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
} AxAcroPDFLib.AxAcroPDF axAcroPDF = new AxAcroPDFLib.AxAcroPDF();
private void Main_Load(object sender, EventArgs e)
{
fileSystemWatcher1.Path = GetPDFDocumentPath();
fileSystemWatcher1.InternalBufferSize = 1024;
axAcroPDF.Location = new System.Drawing.Point(0, 24);
axAcroPDF.Size = new System.Drawing.Size(292, 242);
axAcroPDF.Dock = DockStyle.Fill;
this.Controls.Add(axAcroPDF);
axAcroPDF.setCurrentPage(GetFilePage());//获取要显示的页码
axAcroPDF.LoadFile(GetPDFFilePath(GetPDFDocumentPath()));
} /// <summary>
/// 获取文件夹位置
/// </summary>
/// <returns></returns>
private string GetPDFDocumentPath()
{
string ConfigPath = Application.StartupPath + "/Config/FilePath.ini";
string PDFDocumentPath = "";
FileInfo fif = new FileInfo(ConfigPath);
DirectoryInfo dif = new DirectoryInfo(Application.StartupPath+"/Config");
if (dif.Exists)
{
if (fif.Exists)
{
StreamReader sr = new StreamReader(ConfigPath);
PDFDocumentPath = sr.ReadLine();
sr.Close();
if (PDFDocumentPath.Length < 0)
{
MessageBox.Show("FilePath.ini 文件中没有配置完全!");
}
}
else
{
MessageBox.Show("文件不存在!");
}
}
else
{
MessageBox.Show("文件夹不存在!");
}
return PDFDocumentPath;
} /// <summary>
/// 获取PDF文件路径
/// </summary>
/// <param name="PDFDocumentPath">PDF文件夹路径</param>
/// <returns></returns>
private string GetPDFFilePath(string PDFDocumentPath)
{
string PDFFilePath="";
DirectoryInfo dif = new DirectoryInfo(PDFDocumentPath);
if (dif.Exists)
{
FileInfo[] fif = dif.GetFiles();
foreach (FileInfo PDFFileName in fif)
{
PDFFilePath = PDFDocumentPath + PDFFileName.Name.ToString();
}
}
else
{
MessageBox.Show("PDF存放文件夹已被移动或删除!");
}
return PDFFilePath;
} private void fileSystemWatcher1_Changed(object sender, FileSystemEventArgs e)
{
axAcroPDF.LoadFile(GetPDFFilePath(GetPDFDocumentPath()));
}
/// <summary>
/// 获取文件路径
/// </summary>
/// <returns></returns>
private int GetFilePage()
{
string FilePage = "";
string ConfigPath = Application.StartupPath + "/Config/FilePath.ini";
FileInfo fif = new FileInfo(ConfigPath);
DirectoryInfo dif = new DirectoryInfo(Application.StartupPath+"/Config");
if (dif.Exists)
{
if (fif.Exists)
{
StreamReader sr = new StreamReader(ConfigPath);
sr.ReadLine();
FilePage = sr.ReadLine();
sr.Close();
}
}
if (FilePage==null )
{
return 1;
}
else
{
return Int32.Parse(FilePage);
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;namespace PDFDisplay
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
} AxAcroPDFLib.AxAcroPDF axAcroPDF = new AxAcroPDFLib.AxAcroPDF();
private void Main_Load(object sender, EventArgs e)
{
fileSystemWatcher1.Path = GetPDFDocumentPath();
fileSystemWatcher1.InternalBufferSize = 1024;
axAcroPDF.Location = new System.Drawing.Point(0, 24);
axAcroPDF.Size = new System.Drawing.Size(292, 242);
axAcroPDF.Dock = DockStyle.Fill;
this.Controls.Add(axAcroPDF);
axAcroPDF.setCurrentPage(GetFilePage());//获取要显示的页码
axAcroPDF.LoadFile(GetPDFFilePath(GetPDFDocumentPath()));
} /// <summary>
/// 获取文件夹位置
/// </summary>
/// <returns></returns>
private string GetPDFDocumentPath()
{
string ConfigPath = Application.StartupPath + "/Config/FilePath.ini";
string PDFDocumentPath = "";
FileInfo fif = new FileInfo(ConfigPath);
DirectoryInfo dif = new DirectoryInfo(Application.StartupPath+"/Config");
if (dif.Exists)
{
if (fif.Exists)
{
StreamReader sr = new StreamReader(ConfigPath);
PDFDocumentPath = sr.ReadLine();
sr.Close();
if (PDFDocumentPath.Length < 0)
{
MessageBox.Show("FilePath.ini 文件中没有配置完全!");
}
}
else
{
MessageBox.Show("文件不存在!");
}
}
else
{
MessageBox.Show("文件夹不存在!");
}
return PDFDocumentPath;
} /// <summary>
/// 获取PDF文件路径
/// </summary>
/// <param name="PDFDocumentPath">PDF文件夹路径</param>
/// <returns></returns>
private string GetPDFFilePath(string PDFDocumentPath)
{
string PDFFilePath="";
DirectoryInfo dif = new DirectoryInfo(PDFDocumentPath);
if (dif.Exists)
{
FileInfo[] fif = dif.GetFiles();
foreach (FileInfo PDFFileName in fif)
{
PDFFilePath = PDFDocumentPath + PDFFileName.Name.ToString();
}
}
else
{
MessageBox.Show("PDF存放文件夹已被移动或删除!");
}
return PDFFilePath;
} private void fileSystemWatcher1_Changed(object sender, FileSystemEventArgs e)
{
axAcroPDF.LoadFile(GetPDFFilePath(GetPDFDocumentPath()));
}
/// <summary>
/// 获取文件路径
/// </summary>
/// <returns></returns>
private int GetFilePage()
{
string FilePage = "";
string ConfigPath = Application.StartupPath + "/Config/FilePath.ini";
FileInfo fif = new FileInfo(ConfigPath);
DirectoryInfo dif = new DirectoryInfo(Application.StartupPath+"/Config");
if (dif.Exists)
{
if (fif.Exists)
{
StreamReader sr = new StreamReader(ConfigPath);
sr.ReadLine();
FilePage = sr.ReadLine();
sr.Close();
}
}
if (FilePage==null )
{
return 1;
}
else
{
return Int32.Parse(FilePage);
}
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货