ERROR: System.Environment.SpecialFolder' does not contain a definition for 'CommonApplicationData'

笑着哭i 提交于 2019-12-19 11:57:19

问题


I have the code to save a file in a folder in directory

string timestamp = DateTime.Now.ToString("MM-dd-yyyy.HH-mm-ss");
                var file = File.Create("Owe-Data.txt" + timestamp);

                var com = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase + timestamp + @"\Data" + file;

               MessageBox.Show(com);
               if (!Directory.Exists(com))
                {
                    Directory.CreateDirectory(com);
                }
               using (var sw = new StreamWriter(com))
                {

                    sw.WriteLine(InputData);

                }

            }

i Displayed COM it gives path bt i cant see the Data folder or Owe-Data file at that path Anybody can tell why this happening, or should i save the Data folder in current directory where this prgram running? bt i dnt know how to reach that path. Any solutions ?? Working on windows phone 5, visual studio 2008 .NET framwork 2.0


回答1:


As per the Exceptions section of documentation,the above exception is thrown when

ArgumentException ------- folder is not a member of System.Environment.SpecialFolder.

It means the OS where you are running this command does not have Environment.SpecialFolder.CommonApplicationData as one of the special folder.

For knowledge,
Environment.SpecialFolder.ApplicationData is the most common one. This folder holds per-user, non-temporary application-specific data, other than user documents. A common example would be a settings or configuration file.

Environment.SpecialFolder.CommonApplicationData is similar, but shared across users. You could use this to store document templates, for instance.

Environment.SpecialFolder.LocalApplicationData is a non-roaming alternative for ApplicationData. As such, you'd never store important data there. However, because it's non-roaming it is a good location for temporary files, caches, etcetera. It's typically on a local disk.

I think the problem may be that Environment.SpecialFolder.CommonApplicationData is common and shared between different users and the user with which you have logged in is not having rights to access the folder or the Visual Studio has not been started in Admin mode.

EDIT Look at link and try to add a manual registry Common AppData defined in the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\




回答2:


Given you are asking about a .NET Windows Phone application as per the tags

I think your problem is that a .NET Windows Phone application does not have direct access to the file system; it can only access IsolatedStorage this is by design.

I would quote a Microsoft source for this but I can't seem to find one!

EDIT See this article from MSDN



来源:https://stackoverflow.com/questions/20095060/error-system-environment-specialfolder-does-not-contain-a-definition-for-comm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!