【问题标题】:Folder Browser Dialog with Environment带有环境的文件夹浏览器对话框
【发布时间】:2016-08-30 16:09:07
【问题描述】:

我为我父亲创建了一个应用程序。这只是一个简单的听写程序。问题是当他将它安装在他的计算机上时,它停止并说一般访问被拒绝错误。

第一次出现错误时我使用了SaveFileDialog sfd = new SaveFileDialog() 然后添加了通常的“if 语句”以确保对话框正常。但是应用程序的访问文件被拒绝。

我对@9​​87654323@ 做了同样的事情,它安装在他的电脑上并运行良好。但是,当我在工具箱外使用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;
            }
        }
    }
}

【问题讨论】:

  • 你是你机器上的管理员....他是他机器上的管理员吗?拒绝访问是权限错误。
  • 在实例化OutputPath 的行尾添加+ saveFileDialog1。不是+ saveFileDialog1.FileName 或类似的东西。
  • 我实际上并没有投反对票,而是idownvotedyoubecause.com/so/NoExceptionDetails 认真。如果您有异常,则需要捕获整个异常详细信息并将其添加到您的问题中。否则人们不得不猜测。
  • 我没有得到任何“代表点数”。而且,是的,您需要将异常中的每一条信息都放入您的问题中,以便人们帮助您。这在我提供的链接中得到了充分解释。如果您阅读它而不是将所有内容都如此私人化,那么您将比现在更接近获得答案。但是,嘿,别听我的,我什么都不知道,我只是想提高我的声望。
  • “一般访问被拒绝错误。”

标签: c# winforms


【解决方案1】:

很难说你在哪里失败了,因为尝试/捕获或记录的方式并不多。

您可以改用它,然后粘贴消息框中显示的堆栈跟踪吗?

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 中没有被阻止。

【讨论】:

  • 我能做的是发布整个代码,它有 SaveFileDialog 这样你就可以看到程序的整个代码,它只有大约 100 行代码。此外,这个应用程序只是一个小笔记应用程序。我爸爸不喜欢 MS Speech,因为它会停顿,而且他不喜欢布局。因此,该应用程序只是省略了所有内容并通过该应用程序。它只是一个基本的应用程序。现在Environment 部分在我的自定义桌面屏幕阅读器中。我在win8.1中构建它并将其部署到10。我可以这样做,它只是将文件保存为他键入的内容。那是我的问题。我会将整个代码放在问题中
  • 我仍然会要求您运行此代码并提供详细信息。您提供的内容没有足够的细节来进一步解决这个问题。我要求的代码和详细信息将为我们提供必要的数据。问题显然是环境问题,但没有详细信息,我们无法再为您提供帮助。
  • 我不得不对其进行一些修改,因为它会引发一些通用错误,例如 ;( 是预期的。直到今晚晚些时候,当我将下载链接发送给我父亲时,我才能对其进行测试。我唯一需要测试的另一台笔记本电脑不是在这里,它被送到服务店购买新的主板。所以明天我会发布结果。
  • 已编辑以纠正其中的一些问题。我是徒手写的,所以可能还有更多。
  • 谢谢。您必须对代码进行的任何更改都很重要,因此如果您最终修改它,请同时发布该代码(越少越好!)。
猜你喜欢
  • 2014-07-08
  • 2010-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-29
  • 1970-01-01
  • 1970-01-01
  • 2010-11-18
相关资源
最近更新 更多