احفظ الملف على سطح المكتب في نظام التشغيل Vista/Windows 7 في .NET 2.0

StackOverflow https://stackoverflow.com/questions/1414000

سؤال

أنا أعمل على تحديث أحد تطبيقاتنا.يجب أن يستخدم .NET 2.0.يقوم جزء واحد بإنشاء ملف على سطح المكتب باستخدام

FileStream fs = new FileStream(Environment.GetFolderPath
    (Environment.SpecialFolder.DesktopDirectory), FileMode.Create);

لكنني أحصل على UnauthorizedAccessException في نظام التشغيل Windows 7 (ونظام التشغيل Vista أيضًا، على ما أفترض، على الرغم من أنني لم أختبر ذلك بعد).لقد بحثت في الارتفاع (ليس للبرنامج بأكمله، ولكن للتجميع المنفصل الذي من شأنه إنشاء الملف وتنفيذ الإجراءات عليه)؛ولكن يبدو أن هذا يتطلب .NET 3.0 أو 3.5.هل هناك أي طريقة للوصول إلى مجلد سطح المكتب باستخدام .NET 2.0؟(إن طلب تشغيل البرنامج كمسؤول ليس خيارًا أيضًا)

(لقد أجريت بحثًا، والسؤال الوحيد القريب مما أطرحه هو: فشل إنشاء الملف في الحساب القياسي (Vista) ومع ذلك، فهو يتحدث عن رفع مستوى التطبيق بأكمله وليس خاصًا بـ .NET 2.0، لذلك أعتقد أن هذا ليس نسخة مكررة)

يحرر:
واو ، لقد كنت غبيًا حقًا.هذا في الواقع يعمل بشكل جيد.كنت أحاول إنشاء ملف يسمى C:\Users\MyUser\Desktop.أُووبس.اسف لخلط الامور.

يحرر:وهذا نص الاستثناء:

  System.UnauthorizedAccessException was unhandled
  Message="Access to the path 'C:\\Users\\MyUser\\Desktop' is denied."
  Source="mscorlib"
  StackTrace:
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
       at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
       at System.IO.FileStream..ctor(String path, FileMode mode)
       at MyProgram.Prog.SaveDiagnostic(String filename, String text) in C:\Source\MyProgram\Prog.cs:line 95
       at MyProgram.Form1.buttonGenDiagnostic_Click(Object sender, EventArgs e) in C:\Source\MyProgram\Form1.cs:line 4729
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at Northwoods.CRM.Import.Form1.Main(String[] args) in I:\WebProspect\Source\Northwoods.CRM.Import\Form1.cs:line 2616
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
هل كانت مفيدة؟

المحلول

المشكلة في هذا الكود

FileStream fs = new FileStream(Environment.GetFolderPath
    (Environment.SpecialFolder.DesktopDirectory), FileMode.Create);

دعونا نعيد كتابتها في الخطوات التي ستحدث بالفعل

var desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
var fs = new FileStream(desktopFolder, FileMode.Create);

ما تحاول القيام به هنا ليس إنشاء ملف على سطح المكتب، بل أنت تحاول القيام بذلك قم بإنشاء مجلد سطح المكتب نفسه.من الواضح أن مجلد سطح المكتب موجود بالفعل، لذلك تحصل على خطأ.

ما عليك القيام به هو إنشاء ملف داخل مجلد سطح المكتب.يمكنك استخدام Path.Combine للقيام بذلك، مثل هذا:

var desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
var fullFileName = Path.Combine(desktopFolder, "Test.txt");
var fs = new FileStream(fullFileName, FileMode.Create);

قد ترغب أيضًا في تغيير FileMode إلى OpenOrCreate, ، أو التعامل مع الاستثناءات الخاصة بك - على سبيل المثال، إذا تم تشغيل الكود مرتين، وسيكون الملف موجودًا بالفعل في المحاولة الثانية، لذلك لن تتمكن من إنشائه مرة ثانية

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top