首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mciSendString无法保存到目录路径

mciSendString无法保存到目录路径
EN

Stack Overflow用户
提问于 2009-07-18 17:03:55
回答 3查看 2.5K关注 0票数 0

VS C# 2008 SP1

我创建了一个录制和播放音频的小应用程序。但是,我的应用程序需要将wave文件保存到用户计算机上的应用程序数据目录中。

mciSendString接受C样式字符串作为参数,并且必须是8.3格式。然而,我的问题是我不能保存它。奇怪的是,有时会失败,有时不会。然而,大多数时候都是失败的。但是,如果我直接保存到C盘,它第一次就能正常工作。我已经使用了3种不同的方法,我已经在下面编码。

失败时我得到的错误号是286。“该文件未保存。请确保您的系统有足够的磁盘空间或具有完整的网络连接”

非常感谢你的建议,

代码语言:javascript
复制
[DllImport("winmm.dll",CharSet=CharSet.Auto)]
        private static extern uint mciSendString([MarshalAs(UnmanagedType.LPTStr)] string command,
                                                 StringBuilder returnValue,
                                                 int returnLength,
                                                 IntPtr winHandle);

        [DllImport("winmm.dll", CharSet = CharSet.Auto)]
        private static extern int mciGetErrorString(uint errorCode, StringBuilder errorText, int errorTextSize);

[DllImport("Kernel32.dll", CharSet=CharSet.Auto)]
        private static extern int GetShortPathName([MarshalAs(UnmanagedType.LPTStr)] string longPath,
                                                   [MarshalAs(UnmanagedType.LPTStr)] StringBuilder shortPath,
                                                    int length);






 // Stop recording
        private void StopRecording()
        {
            // Save recorded voice
            string shortPath = this.shortPathName();
            string formatShortPath = string.Format("save recsound \"{0}\"", shortPath);
            uint result = 0;
            StringBuilder errorTest = new StringBuilder(256);

            // C:\DOCUME~1\Steve\APPLIC~1\Test.wav
            // Fails
            result = mciSendString(string.Format("{0}", formatShortPath), null, 0, IntPtr.Zero);
            mciGetErrorString(result, errorTest, errorTest.Length);

            // command line convention - fails 
            result = mciSendString("save recsound \"C:\\DOCUME~1\\Steve\\APPLIC~1\\Test.wav\"", null, 0, IntPtr.Zero);
            mciGetErrorString(result, errorTest, errorTest.Length);         

            // 8.3 short format - fails
            result = mciSendString(@"save recsound C:\DOCUME~1\Steve\APPLIC~1\Test.wav", null, 0, IntPtr.Zero);
            mciGetErrorString(result, errorTest, errorTest.Length);

            // Save to C drive works everytime.
            result = mciSendString(@"save recsound C:\Test.wav", null, 0, IntPtr.Zero);
            mciGetErrorString(result, errorTest, errorTest.Length);

            mciSendString("close recsound ", null, 0, IntPtr.Zero);
        }


// Get the short path name so that the mciSendString can save the recorded wave file
        private string shortPathName()
        {
            string shortPath = string.Empty;
            long length = 0;
            StringBuilder buffer = new StringBuilder(256);

            // Get the length of the path 
            length = GetShortPathName(this.saveRecordingPath, buffer, 256);

            shortPath = buffer.ToString();

            return shortPath;
        }
EN

回答 3

Stack Overflow用户

发布于 2012-09-08 21:32:56

我在C driveD drive中测试了它,只要目录路径不包含空格,它就可以工作。"C:\Test.wav""D:\mp3\test.wav"都可以工作,但"D:\mp4 folder\test.wav"不能工作。

票数 3
EN

Stack Overflow用户

发布于 2010-04-18 10:37:22

试试这个:

Directory.SetCurrentDirectory(directoryString);directoryString =“C:\mciSendString~1\Steve\APPLIC~1\”;string mciSendString(@“保存Test.wav",null,0,IntPtr.Zero);mciSendString("close recsound ",null,0,IntPtr.Zero);

票数 1
EN

Stack Overflow用户

发布于 2021-11-27 16:55:56

如果文件路径中有空格,则在文件名两边使用引号。在C#中,如果您使用filePath作为文件路径的变量,则使用以下代码:

代码语言:javascript
复制
msiSendString($"save recsound \"{filePath}\"", "", 0, 0)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1148022

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档