小弟初学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.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);
}
}
}
}
解决方案 »
- 关于B/S C/S
- 急急急急急急急急急急急急急急急急!!!操作excel问题
- 【关于网银支付的借口问题】
- C# 求一价格提取正则
- 简单登录问题????
- Socket通信,在Windows 2000下没问题,在2003下遇到问题。
- 怎样将XmlDocument类型的变量转换为string类型?
- 关于图形旋转的算法。
- ArrayList 问题
- [求助]如何把datagrid中得到的数据导出为Excel电子表格的形式?!
- VS2008 添加数据源看不到SqlServer Compact选项
- 无法将类型“System.Data.DataSet”隐式转换为“WebApplication1.DataSet2” 在线等,请各位高手帮忙
貌似多个线程同时访问一个文件不能像LZ这样处理的
如果要多个线程访问,可以先将PDF复制一个,给个随机文件名,去访问副本,访问结束再把它删除也许可以解决LZ的问题