【问题标题】:Why i'm getting exception InvalidDeploymentException when running the program?为什么我在运行程序时收到异常 InvalidDeploymentException?
【发布时间】:2016-11-07 01:49:08
【问题描述】:

我找不到我在哪里使用记录器。肯定不在 Form1 构造函数中。 所以我无法弄清楚为什么它首先会到达这个模块以及为什么它会抛出这个异常。

这是模块代码:

/*----------------------------------------------------------------
 * Module Name  : Logger
 * Description  : A logger
 * Author       : Danny
 * Date         : 10/02/2010
 * Revision     : 1.00
 * --------------------------------------------------------------*/


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;
/*
 *  Introduction :
 * 
 *  This module is a logger. any module can use this 
 *  module to log its actions.
 * 
 * 
 * */

        /*----------------------------------------
         *   P R I V A T E    D E F I N I T I O N S 
         * ---------------------------------------*/


namespace DannyGeneral
{
    class Logger
    {
        /*----------------------------------------
         *   P R I V A T E     C O N S T A N T S 
         * ---------------------------------------*/
        static string log_file_name = @"\logger.txt";
        static string full_path_log_file_name;
        static string path_log;
        static Mutex mut;
        /*----------------------------------------
         *   P R I V A T E     V A R I A B L E S 
         * ---------------------------------------*/

        /*---------------------------------
         *   P U B L I C   M E T H O D S 
         * -------------------------------*/

        /*----------------------------------------------------------
         * Function     : Logger
         * Description  : static Constructor
         * Parameters   : none
         * Return       : none
         * --------------------------------------------------------*/
        static Logger()
        {
            mut = new Mutex();
            path_log = Path.GetDirectoryName(Application.LocalUserAppDataPath)+ @"\log";
            if (!Directory.Exists(path_log))
            {
                Directory.CreateDirectory(path_log);
            }
            full_path_log_file_name = path_log + log_file_name;
        }

        /*----------------------------------------------------------
         * Function     : Write
         * Description  : writes a string to the log file
         *                This functions will add time and date and
         *                end of line chars to the string written to
         *                the file.
         * Parameters   : string to write to the file.
         * Return       : none
         * --------------------------------------------------------*/
        public static void Write(string str)
        {
            if (mut.WaitOne() == false)
            {
                return;
            }
            else
            {

                using (StreamWriter sw = new StreamWriter(full_path_log_file_name, true))
                {
                    sw.Write(DateTime.Now.ToShortDateString() + "--" + DateTime.Now.ToShortTimeString() + " ==> " + str);
                    sw.WriteLine();
                    sw.Close();
                }
            }
            mut.ReleaseMutex();
        }
        public static void exist()
        {
            if (!File.Exists(path_log + log_file_name))
            {
                StreamWriter sw = new StreamWriter(path_log + log_file_name);
                sw.Write(DateTime.Now.ToShortDateString()+"--"+DateTime.Now.ToShortTimeString()+" ==> "+"First Time The Log File Was Created"+Environment.NewLine);
                sw.WriteLine();
                sw.Close();
            }
        }
        public static void newEmptyLine()
        {
            StreamWriter sw = new StreamWriter(path_log + log_file_name,true);
            sw.WriteLine();
            sw.Close();
        }

        /*---------------------------------
         *   P R I V A T E    M E T H O D S 
         * -------------------------------*/

    }
}

异常上线:

path_log = Path.GetDirectoryName(Application.LocalUserAppDataPath)+ @"\log";

StackTrace:在 System.Deployment.Application.ApplicationDeployment.get_CurrentDeployment()

另一件事可能是在运行程序后有一种方法,异常被抛出以某种方式找到程序中的什么/在哪里被称为这个模块?

当我使用调试断点时

path_log = Path.GetDirectoryName(Application.LocalUserAppDataPath)+ @"\log";

并将鼠标放在:LocalUserAppDataPath 我看到了路径:C:\Users\Chocolade\AppData\Local\Youtube_Manager\Youtube-Manager\1.0.0.0

但为什么它会使用 1.0.0.0 显示/显示路径?应该只有:C:\Users\Chocolade\AppData\Local\Youtube_Manager\Youtube-Manager

不知道发生了什么。

【问题讨论】:

    标签: c# .net winforms


    【解决方案1】:

    你可以替换

    Path.GetDirectoryName(Application.LocalUserAppDataPath)
    

    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多