mciSendString无法保存到目录路径
问题描述:
VS C#2008 SP1mciSendString无法保存到目录路径
我创建了一个小应用程序,可以记录和播放音频。但是,我的应用程序需要将wave文件保存到用户计算机上的应用程序数据目录中。
mciSendString将C样式字符串作为参数,并且必须采用8.3格式。但是,我的问题是我无法保存它。奇怪的是有时它确实有时并不是。无论如何,大部分时间都是失败。但是,如果我直接保存到C驱动器,它首次运行所有内容。我已经使用了3种不同的方法,我在下面编码。
,我得到的,当它失败的错误编号是286.“文件没有保存。请确保您的系统有足够的磁盘空间或具有完整的网络连接”
非常感谢任何suggestins,
[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;
}
答
尝试:
[ 串directoryString = “C:\ DOCUME〜1 \史蒂夫\ APPLIC〜1 \”; Directory.SetCurrentDirectory(directoryString);mciSendString(@“save recsound Test.wav”,null,0,IntPtr.Zero);mciSendString(“close recsound”,null,0,IntPtr.Zero); ]
答
我测试它在C drive
和D drive
,只要目录路径不包含空格,它就会工作。 "C:\Test.wav"
和"D:\mp3\test.wav"
都可以工作,但"D:\mp4 folder\test.wav"
不起作用。
谢谢,我会试一试。 – ant2009 2010-04-21 06:11:05