我正在使用Windows 7上的winforms,在Visual Studio 2010中使用C#。 目前在Windows 7中安装以及从调试器安装下面的代码。但是,当程序安装在Windows XP中时,永远不会到达最后一行。SaveFileDialog无法在Windows XP中返回
此代码从MenuStrip调用,然后传递给后续方法以根据菜单中单击的项执行操作。但是,这不是SaveFileDialog失败的唯一地方,并且它始终在ShowDialog()方法上失败。
代码为菜单项:即失败
private void saveOnlyPlaylistToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
MainMenuClick(sender, e);
}
catch (Exception ex)
{
StackTrace st = new StackTrace();
string methodName = st.GetFrame(1).GetMethod().Name;
Logger.LogToFile("Failure in " + methodName + ": " + ex.Message);
}
}
代码:
Logger.LogToFile("Entered Save Only Playlist.");
SaveFileDialog sfd = new SaveFileDialog();
string playlistSaveLocation = config["PlaylistLocation"];
if (!Directory.Exists(playlistSaveLocation))
Directory.CreateDirectory(playlistSaveLocation);
sfd.InitialDirectory = playlistSaveLocation;
sfd.Filter = "L Playlist (*.lpl)|*.lpl";
DialogResult result = sfd.ShowDialog();
Logger.LogToFile("Result of Dialog: " + result.ToString());
我想不通,为什么最后一行没有被调用,感觉就像ShowDialog的() SaveFileDialog的方法没有完成。之后程序继续正常运行,但不再与文件目录交互,也无法创建新进程。
调试器中没有错误,也没有事件日志。我已经使用Visual Studio 2010在Windows XP上重建了它,它运行良好,错误似乎只在程序在Windows 7中创建并在Windows XP上安装时出现。 Windows 7是64位。
我应该注意到,我从调试 - >例外引发了所有异常。
我周围搜索,似乎并没有像SaveFileDialog失败,有什么可能会导致此问题的任何想法许多情况下的基础方法?
基于以下答案的进一步分析导致我相信这可能与如何调用SaveFileDialog有关。由于这是从MenuStrip调用的,我相信它是作为一个单独的线程出现的。这可能是ShowDialog()方法永远不会返回的原因,但我无法确定这是为什么。为了找到问题,我创建了一个单独的窗口窗体,它除了有一个按钮来打开保存文件对话框。这个按钮可以正常工作,并且返回正确,但是在将控制返回到原始线程时,它似乎再次失败。这全部基于我为调试目的而设置的日志记录。
记录代码:
public static void LogToFile(string message, FileInfo fInfo)
{
try
{
if (!fInfo.Exists)
using (FileStream fs = fInfo.Create()) ;
message = DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss") + ": " + message;
File.AppendAllText(fInfo.FullName, message + "\n");
}
catch (Exception ex)
{
StackTrace st = new StackTrace();
string methodName = st.GetFrame(1).GetMethod().Name;
MessageBox.Show("Failure in " + methodName + ": " + ex.Message);
}
}
代码附加窗口:
public partial class Buffer : Form
{
public Buffer()
{
InitializeComponent();
}
private void Buffer_Load(object sender, EventArgs e)
{
Logger.LogToFile("Entered Save Only Playlist.");
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Playlist (*.lpl)|*.lpl";
DialogResult result = System.Windows.Forms.DialogResult.Cancel;
try
{
result = sfd.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show("Dialog problem: " + ex.Message);
}
Logger.LogToFile("Result of Dialog: " + result.ToString());
MessageBox.Show("Result of Dialog: " + result.ToString());
DialogResult = result;
}
}
上述工程的代码,和MessageBox.Show()实际显示的对话框的结果。
常见的对话框使用COM,并且如果您不在单线程的公寓中,则以各种奇怪的方式运行。你的'Main'方法有'[STAThread]'属性吗? –
嘿乔,是的,我有[STAThread]我的主: \t \t \t \t [STAThread] \t \t \t \t静态无效的主要() \t \t \t \t { \t \t \t \t \t Application.EnableVisualStyles() ; \t \t \t \t \t Application.SetCompatibleTextRenderingDefault(false); \t \t \t \t \t Application.Run(new frmLTSBC()); \t \t \t \t} – Direweasel
如果你注释掉“string playlistSaveLocation ...”和接下来的两行,会发生什么? Win7计算机上播放列表保存位置的值是多少?也许UAC安全性是干扰性的。 – LarsTech