我正在使用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