【问题标题】:Compact Framework Current FolderCompact Framework 当前文件夹
【发布时间】:2010-11-09 12:53:51
【问题描述】:

我如何知道哪个是 App 的当前文件夹?

我的意思是...有没有办法从正在运行的代码中知道 exe 位于何处?

【问题讨论】:

    标签: c# vb.net windows-mobile compact-framework


    【解决方案1】:
    string fullAppName = Assembly.GetCallingAssembly().GetName().CodeBase;
    string fullAppPath = Path.GetDirectoryName(fullAppName);
    

    【讨论】:

      【解决方案2】:

      以下是正确的。

      string fullAppName = Assembly.GetCallingAssembly().GetName().CodeBase;
      fullAppPath = Path.GetDirectoryName(fullAppName);
      有关其他语言的等效代码,请参阅此link

      【讨论】:

        【解决方案3】:

        您可以使用GetModuleFileName

        在下面的示例中,方法GetExecutablePath 返回exe 的位置,GetStartupPath 返回exe 的目录。

        using System;
        using System.ComponentModel;
        using System.IO;
        using System.Runtime.InteropServices;
        using System.Text;
        
        class Program
        {
            [DllImport("coredll", SetLastError = true)]
            public static extern uint GetModuleFileName(IntPtr hModule, StringBuilder lpFilename, [MarshalAs(UnmanagedType.U4)] int nSize);
        
            [DllImport("coredll")]
            public static extern uint FormatMessage([MarshalAs(UnmanagedType.U4)] FormatMessageFlags dwFlags, IntPtr lpSource, uint dwMessageId, uint dwLanguageId, out IntPtr lpBuffer, uint nSize, IntPtr Arguments);
        
            [DllImport("coredll")]
            public static extern IntPtr LocalFree(IntPtr hMem);
        
            [Flags]
            internal enum FormatMessageFlags : uint
            {
                AllocateBuffer = 256,
                FromSystem = 4096,
                IgnoreInserts = 512
            }
        
            public static string GetModuleFileName(IntPtr hModule)
            {
                StringBuilder lpFilename = new StringBuilder(short.MaxValue);
                uint num = GetModuleFileName(hModule, lpFilename, lpFilename.Capacity);
                if (num == 0)
                {
                    throw CreateWin32Exception(Marshal.GetLastWin32Error());
                }
                return lpFilename.ToString();
            }
        
            private static Win32Exception CreateWin32Exception(int error)
            {
                IntPtr buffer = IntPtr.Zero;
                try
                {
                    if (FormatMessage(FormatMessageFlags.IgnoreInserts | FormatMessageFlags.FromSystem | FormatMessageFlags.AllocateBuffer, IntPtr.Zero, (uint)error, 0, out buffer, 0, IntPtr.Zero) == 0)
                    {
                        return new Win32Exception();
                    }
                    return new Win32Exception(error, Marshal.PtrToStringUni(buffer));
                }
                finally
                {
                    if (buffer != IntPtr.Zero)
                    {
                        LocalFree(buffer);
                    }
                }
            }
        
            public static string GetStartupPath()
            {
                return Path.GetDirectoryName(GetExecutablePath());
            }
        
            public static string GetExecutablePath()
            {
                return GetModuleFileName(IntPtr.Zero);
            }
        }
        

        【讨论】:

        • 对于这么小的钉子来说,这真是一把大锤子。
        【解决方案4】:

        不要与系统对抗。

        Microsoft 不希望您将程序文件文件夹用于除程序集以外的任何内容。配置文件应该放在Application Data,Save文件等用户需要知道的放在My Documents中。

        jalf 的回答会起作用,但你正在与系统作斗争。除非他们是你想知道你的程序集在哪个文件夹中的一个很好的理由,否则我建议不要这样做。

        【讨论】:

          【解决方案5】:

          Windows Mobile 没有当前文件夹的概念。 “当前文件夹”基本上总是设置为文件系统的根目录,无论您的应用程序位于何处。

          要获取您的应用程序所在的路径,您可以使用Assembly.GetExecutingAssembly(),以及CodeBase 属性或GetName() 方法

          【讨论】:

            猜你喜欢
            • 2012-02-12
            • 2011-08-23
            • 2010-11-01
            • 1970-01-01
            • 2011-01-03
            • 2012-04-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-22
            相关资源
            最近更新 更多