文件夹浏览器对话框与环境

本文关键字:环境 对话框 浏览器 文件夹 | 更新日期: 2023-09-27 18:04:41

我为我爸爸创建了一个应用程序。这只是一个简单的听写程序。问题是,当他把它安装到他的电脑上时,它卡住了,说一般访问拒绝错误。

第一次给出错误,我使用SaveFileDialog sfd = new SaveFileDialog(),然后添加通常的"if语句",以确保对话框是ok的。然而,该应用程序有一个访问文件被拒绝。

我对Environment.GetFolder做了同样的事情,它安装在他的计算机上的位置并运行良好。然而,当我使用saveFileDialog1openFileDialog1的工具箱,它不保存或打开一个txt文档。

它在我的笔记本电脑上工作,而不是他的。这可能是由于代码与他的计算机中的错误。还有什么是正确的方法来使用Environement.GetFolderSaveFileDialog .

如果需要,我也可以将完整的代码发布到程序中。

  private void lblOpenFile_Click(object sender, EventArgs e)
    {
        OpenFileDialog open = new OpenFileDialog();
        open.Title = "Open File";
        open.Filter = "Text Files (*txt) | *.txt";
        if (open.ShowDialog() == DialogResult.OK)
        {
            StreamReader read = new StreamReader(File.OpenRead(open.FileName));
            txtTextBox.Text = read.ReadToEnd();
            read.Dispose();
        }
    }
    private void lblSaveFile_Click(object sender, EventArgs e)
    {
        SaveFileDialog save = new SaveFileDialog();
        save.Title = "Save File";
        save.Filter = "Text Files (*txt) | *.txt";
        if (save.ShowDialog() == DialogResult.OK)
        {
            StreamWriter write = new StreamWriter(File.Create(save.FileName));
            write.Write(txtTextBox.Text);
            write.Dispose();
        }
    }

这是我在屏幕录像机上使用的Enviroment。当我点击保存时,它会弹出一个对话框,我输入文件名,点击保存,它什么都不做。它保存文件,但不是我指定的。所以我试图合并上面和下面的代码。上面的代码不能授予访问权限,但是下面的代码可以

string OutputPath;    
OutputPath = Environment.GetFolderPath(Environment.SpecialFolder.MyVideos) + @"''IvanSoft Desktop Recorder" + saveFileDialog1;

private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
    {
       string fileName = saveFileDialog1.FileName;
        fileName = "Tutorial";       
    }

程序的全部代码

using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Speech.Recognition;
using System.Threading;
namespace AGM_Speech
{
    public partial class Form1 : Form
    {
        public SpeechRecognitionEngine recognizer;
        public Grammar grammar;
        public Thread RecThread;
        public Boolean RecognizerState = true;
        public Form1()
        {
            InitializeComponent();
        }
        private void lblAbout_Click(object sender, EventArgs e)
        {
            this.Hide();
            About about = new About();
            about.Show();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            GrammarBuilder builder = new GrammarBuilder();
            builder.AppendDictation();

            grammar = new Grammar(builder);
            recognizer = new SpeechRecognitionEngine();
            recognizer.LoadGrammarAsync(grammar);
            recognizer.SetInputToDefaultAudioDevice();
            recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
            RecognizerState = true;
            RecThread = new Thread(new ThreadStart(RecThreadFunction));
            RecThread.Start();
        }
        private void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            if (!RecognizerState)
                return;
            this.Invoke((MethodInvoker)delegate
            {
                txtTextBox.Text += (e.Result.Text.ToLower() + " ");
                txtTextBox.SelectionStart = txtTextBox.Text.Length - 0;
                txtTextBox.SelectionLength = 0;
            });
        }
        public void RecThreadFunction()
        {
            while (true)
            {
                try
                {
                    recognizer.RecognizeAsync();
                }
                catch
                {
                }

            }
        }
        private void lblStartSpeech_Click(object sender, EventArgs e)
        {
            RecognizerState = true;
        }
        private void lblStopSpeech_Click(object sender, EventArgs e)
        {
            RecognizerState = false;
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            RecThread.Abort();
            RecThread = null;
            grammar = null;
            recognizer.UnloadAllGrammars();
            recognizer.Dispose();

        }
        private void lblOpenFile_Click(object sender, EventArgs e)
        {
            string open = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            StreamReader reader = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));

        }
        private void lblSaveFile_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.Title = "Save File";
            save.Filter = "Text Files (*txt) | *.txt";
            if (save.ShowDialog() == DialogResult.OK)
            {
                StreamWriter write = new StreamWriter(File.Create(save.FileName));
                write.Write(txtTextBox.Text);
                write.Dispose();
            }
        }


        private void txtSearch_Click(object sender, EventArgs e)
        {
            txtSearch.Clear();
            lblGo_Click(null, null);
        }
        private void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)ConsoleKey.Enter)
            {
                lblGo_Click(null, null);
            }
        }
        private void lblGo_Click(object sender, EventArgs e)
        {
            int index = 0;
            String temp = txtTextBox.Text;
            txtTextBox.Text = "";
            txtTextBox.Text = temp;
            while (index <= txtTextBox.Text.LastIndexOf(txtSearch.Text))
            {
                txtTextBox.Find(txtSearch.Text, index, txtTextBox.TextLength, RichTextBoxFinds.None);
                txtTextBox.SelectionColor = Color.YellowGreen;
                index = txtTextBox.Text.IndexOf(txtSearch.Text, index) + 1;
            }
        }
    }
}

文件夹浏览器对话框与环境

很难说你在哪里失败了,因为没有太多的try/catch或日志记录。

您可以使用此方法,并粘贴消息框中显示的堆栈跟踪吗?

private string fileOutputLocation { get; set; }
private void lblSaveFile_Click(object sender, EventArgs e)
{
    bool fileSelected = false;
    Try(() =>
    {
      SaveFileDialog save = new SaveFileDialog();
      save.Title = "Save File";
      save.Filter = "Text Files (*txt) | *.txt";
      save.InitialDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyVideos), "IvanSoft Desktop Recorder");
      if (save.ShowDialog() == DialogResult.OK)
      {
         fileOutputLocation = save.FileName;
         fileSelected = true;
      }
   });
   if (fileSelected)
   {
      bool fileSaved = SaveFile();
      MessageBox.Show("File saved successfully: " + fileSaved.ToString());
   }
}
private bool SaveFile()
{
   TryDeleteFile();
   bool fileSaved = false;
   Try(()=>
   {
     File.WriteAllText(fileOutputLocation, txtTextBox.Text);
     fileSaved = true;
   });
   return fileSaved;
}
private void TryDeleteFile()
{
   Try(()=>
   {
      if (File.Exists(fileOutputLocation))
      {
         File.Delete(fileOutputLocation);
      }
   });
}
private void Try(Action action)
{
    try
    {
        action();
    }
    catch (Exception e)
    {
        MessageBox.Show(string.Format("The following exception was thrown:'r'n{0}'r'n'r'nFile path: {1}", e.ToString(), fileOutputLocation));
    }
}

有了这个,再加上记录到Windows事件查看器(开始>运行>Eventvwr>安全)的事件,我们应该能够告诉你问题是什么。

最后,如果你只是提供一个可执行文件来运行,你应该检查文件的属性,以确保它在Windows中没有被阻塞。